首页 > 解决方案 > 我对数据类型感到困惑

问题描述

我正在研究一个 Hack Reactor 准备程序问题,问题如下所列。我的问题是 customerData 的数据类型是什么?它似乎是一个对象,但每个属性前面都有一个名称“Joe”,然后是一个键值对。我认为 JavaScript 对象具有带有键值对的属性。

问题:

编写一个名为“greetCustomer”的函数。

给定一个名称,“greetCustomer”会根据该客户访问餐厅的次数返回一个问候语。请参考 customerData 对象。

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

Case 1 - Unknown customer ( Name is not present in customerData ): 

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

Case 2 - Customer who has visited only once ( 'visits' value is 1 ):

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

Case 3 - Repeat customer: ( 'visits' value is greater than 1 ):

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

Notes:
* Your function should not alter the customerData object to update the number of visits.
* Do not hardcode to the exact sample data. This is a BAD IDEA:


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

Starter Code :
*/

var customerData = {
  'Joe': {
    visits: 1
  },
  'Carol': {
    visits: 2
  },
  'Howard': {
    visits: 3
  },
  'Carrie': {
    visits: 4
  }
};

function greetCustomer(firstName) {
  var greeting = '';
  // your code here

  return greeting;
}

标签: javascript

解决方案


它是一个对象。

{对象字面量的语法}包含一组键:值对,以逗号分隔。

键可以是字符串或标识符(在这种情况下它们是字符串),值可以是解析为值的任何表达式(在这种情况下:更多对象文字)。


推荐阅读