写一个名为“hiCustomer”的函数 - JS

时间:2017-05-03 10:28:11

标签: javascript object

根据名称,“hiCustomer”会根据客户访问餐厅的次数返回问候语。请参阅customerData对象。

问候语应该有所不同,具体取决于他们预订的名称。

案例1 - 未知客户(customerData中不存在名称):

var output = hiCustomer('Nick');
console.log(output); // --> 'Welcome! Is this your first time?'

案例2 - 仅访问过一次的客户('访问'值为1):

var output = hiCustomer('Mark');
console.log(output); // --> 'Welcome back, Mark! We're glad you liked us the first time!'

案例3 - 重复客户:('访问'值大于1):

var output = hiCustomer('Hace');
console.log(output); // --> 'Welcome back, Hace! So glad to see you again!'

备注: *您的函数不应更改customerData对象以更新访问次数。 *不要硬编码到确切的样本数据。这是一个不好的想法:

if (firstName === 'Joe') {
  // do something
}

入门代码:

var customerList = {
  'Mark': {
    visits: 1
  },
  'Hace': {
    visits: 2
  },
  'Grace': {
    visits: 3
  },
  'Ruby': {
    visits: 4
  }
};

function hiCustomer(firstName) {
  var greeting = '';

  // your code here

  return greeting;
}

1 个答案:

答案 0 :(得分:0)

我根据您的要求创建了一个示例。请查看running code

<!DOCTYPE html>
<html>

<body>
<label for='numinput'>Display Message :</label>
<input type='text' id='numinput' name='numinput'></input>
<div id='output'></div>

<script>
var greeting;
var customerList = {
  'Mark': {
    visits: 1
  },
  'Hace': {
    visits: 2
  },
  'Grace': {
    visits: 3
  },
  'Ruby': {
    visits: 4
  }
};
var message=[
'Welcome! Is this your first time?',
'Welcome back, <name>! We\'re glad you liked us the first time!','Welcome back, <name>! So glad to see you again!']

function hiCustomer(firstname) {
if(customerList[firstname.target.value])
{
    greeting = message[customerList[firstname.target.value].visits >1 ? 2 : customerList[firstname.target.value].visits].replace('<name>',firstname.target.value);
}else{
    greeting = message[0];
}
    output.innerHTML = greeting;
}



var button = document.getElementById('changeText');
var output = document.getElementById('output');
var input = document.getElementById('numinput');
input.addEventListener('input', hiCustomer);
</script>

</body>
</html>