我不想输入所有可能的价格组合,但我一直在搜索很长时间,找不到更好的方法:
var selection = new Array (4);
selection[0] = new Array ('$210' 'etc', 'etc', 'etc', 'etc');
selection[1] = new Array ('Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole');
selection[2] = new Array ('Talk', 'Talk & Text', 'Talk, Text & Data');
selection[3] = new Array ('One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four Years', 'Four');
function selectPhone () {
var yourPhone = prompt("What kind of Smartphone would you like: Solar: $100, Supernova: $200, Quasar: $300, Galaxy: $400, Blackhole: $500?");
if (yourPhone == selection[1][0]) {
console.log("You picked: " + yourPhone + "."), selectPlan ();
} else {
console.log("Error.");
}
}
function selectPlan () {
var yourPlan = prompt("What Plan Would You Like: Talk: $10, Talk & Text: $20 or Talk, Text & Data: $30?");
if (yourPlan == selection[2][0]) {
console.log("You picked: " + yourPlan + "."), selectTerm ();
} else {
console.log("Error.");
}
}
function selectTerm () {
var yourTerm = prompt("What Term Would You Like: One Year: $100, Two Years: $200, Three Years: $300 or Four Years: $400?");
if (yourTerm == selection[3][0] || selection [3][1]) {
console.log("You picked: " + selection[3][0] + ". \n Your total is: " + selection[0][0]);
} else {
console.log("Error.");
}
}
selectPhone ();
我无法弄清楚如何对它进行编程,因此它可以选择所做的选择并将它们转换为数值并对它们执行简单的添加。我是初学者所以请解释一切。感谢很多!!
答案 0 :(得分:1)
你可以使用像parseInt()这样你需要转换为整数的东西。这是文档https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
答案 1 :(得分:-1)
你这样做:
var selection = new Array (4);
selection[0] = new Array ('$210', 'etc', 'etc', 'etc', 'etc');
selection[1] = new Array ('Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole');
selection[2] = new Array ('Talk', 'Talk & Text', 'Talk, Text & Data');
selection[3] = new Array ('One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four Years', 'Four');
function selectPhone (selectionArray) {
var yourPhone = prompt("What kind of Smartphone would you like: Solar?");
for( s1 in selectionArray ){
for( s2 in selectionArray[s1] ) {
if (yourPhone == selectionArray[s1][s2] ) {
console.log("You picked: " + yourPhone + ".");
} else {
console.log("Error.");
}
}
}
}
selectPhone(selection);
你确实需要知道对象。另请查看JS中的for .. in
函数 - 文档将比我更好地解释它,但简而言之,您可以遍历父数组的属性(使用for..in) - 基本上通过数组列表。比使用相同的想法,你可以部分地遍历每个数组。为了给你一个直观的表示,你的数组看起来像这样:
var selection = [//outer array
[ //inner array - property of outer array (first for..in) = s1
'$210', //property of inner array (second for..in) = s2
'etc'
],
[ 'Solar', 'Supernova', 'Quasar', 'Galaxy', 'Blackhole' ],
[ 'Talk', 'Talk & Text', 'Talk, Text & Data' ],
[ 'One Year', 'One', 'Two Years', 'Two', 'Three Years', 'Three', 'Four' ]
]