我的阵列不返回,我做错了什么吗?
代码如下:
console.log(rest[1]);
var rest = ["First Restaurant","Second Restaurant", "Third Restaurant"];
答案 0 :(得分:1)
首先,考虑元素的顺序,在声明它之前,您具有console.log(rest [1])。诸如calculate()之类的函数将被提升到顶部,这意味着您可以在第一行中将其安全地分配给merged
。
第二,您拥有:
>>> b_items = dictB.items()
>>> merged = dict(pair for pair in dictA.items() if pair in b_items)
您将看到restPrice不会被分配为var price = calculate()
,您使用的是分号而不是逗号:
var rest = ["First Restaurant", "Second Restaurant", "Third Restaurant"];
restPrice = new Array(124, 48, 268);
尽管您对变量和函数提升的顺序有所了解,但我建议改成这样写文件:
var
注意:该函数声明为var rest = ["First Restaurant", "Second Restaurant", "Third Restaurant"],
restPrice = new Array(124, 48, 268);
,您是说function calcute(bill) {
if (bill < 50) {
return bill * 0.2;
} else if (bill >= 50 && bill < 200) {
return bill * 0.15;
} else if (bill > 200) {
return bill * 0.1;
}
}
var price = calcute();
var rest = ["First Restaurant", "Second Restaurant", "Third Restaurant"];
var restPrice = new Array(124, 48, 268);
console.log(rest[1]); // Outputs: "Second Restaurant"
console.log(restPrice); // Outputs: [ 124, 48, 268 ]
var cal = calcute(restPrice[0]);
console.log('Cal: ', cal); // Outputs: Cal: 18.599999999999998
吗?