javascript - 我对数据类型感到困惑
问题描述
我正在研究一个 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;
}
解决方案
它是一个对象。
{
对象字面量的语法}
包含一组键:值对,以逗号分隔。
键可以是字符串或标识符(在这种情况下它们是字符串),值可以是解析为值的任何表达式(在这种情况下:更多对象文字)。
推荐阅读
- python - 如何在 Python 中获得 R 的 ccf?
- ruby-on-rails - Active Model Serializer 不使用 JSON:API 适配器
- scala - 符号'术语
类路径中缺少 .shapeless'。这个符号是 'value blink.linalg.operators.MatrixGenericOps.v1ne' 所必需的 - python - 在 max_value 设置为 None 的情况下使用 Keras 的剪辑功能是否安全?
- android - 如何修复此错误:“../node_modules/react-native/scripts/third-party/glog-0.3.5/test-driver”。无法遵循符号链接
- ms-access - 如何在 MS Access 的单个查询中找到一个字段中的最小值和另一个字段中的关联值?
- django - 如何在模板中显示两个表单的对象
- java-web-start - 不再显示发布者 Java Web Start 的消息
- datatable - DataTables - 在父行顶部打开子行
- angular - Angular 6 延迟循环