这让我疯狂!我很新的JavaScript(可以阅读它,但不总是写)我的问题是三重。
用户需要输入(提示)“高单拿铁”。
1.我想给这个问题添加一个数组来存储。)咖啡串和b)咖啡的价格。
2.我想用for循环输出迄今为止订购的咖啡总量。
3.我的输出应该是表格格式,例如。
短单拿铁的价格为R10
双高大咖啡的价格是R15
Javascript:通过阵列循环
var coffee = [ ];
var price = [ ];
for (var i = 0; i < 2; i++) {
var coffee = prompt("What coffee do you want:", "");
// Size
if (coffee.indexOf('short') > -1) {
var size = 7;
}
if (coffee.indexOf('tall') > -1) {
var size = 9;
}
if (coffee.indexOf('grande') > -1) {
var size = 11;
}
// Shots
if (coffee.indexOf('single') > -1) {
var shots = 1;
}
if (coffee.indexOf('double') > -1) {
var shots = 2;
}
if (coffee.indexOf('triple') > -1) {
var shots = 3;
}
// Is cappuccino?
if (coffee.indexOf('cappuccino') > -1) {
var extra = 2;
} else {
var extra = 0;
}
var price = (size + (3 * shots) + extra);
console.log(coffee + "'s price is R" + price);
}
什么,我想实现的一个例子:
var coffee = [ ];
var price = [ ];
coffee.push("short single latte");
price.push(10);
coffee.push("double tall latte");
price.push(15);
var i;
for (i = 0; i < coffee.length ; i++)
{
console.log(coffee[i] + "'s price is R" + price[i]);
}
您可以完全限定数组,当你做它,就像'VAR大小= ['large','medium','small']',它可能对你的“咖啡”使用一个对象而不是数组是有意义的:'coffee:{sizes:[...],prices: [...],...};然后访问'coffee.sizes.indexOf(userinput [0])' – 2014-11-22 00:21:29
@Lashane这样的东西没有任何意义。 OP使用'var'是正确的。虽然把'var's放入'if's是主观的。 – soktinpk 2014-11-22 00:52:50
@soktinpk'var shots' x 3,'var size' x 3;它不会抛出错误,但它不是_correct_。 – Mathletics 2014-11-22 00:54:18