我正在编写一些javascript,而且我被卡住了。这是代码:
var options = confirm ("Here's our menu. we have chicken, rice, eggs (boiled, fried or served uncooked), meat (choose your type of meat (you can take it raw, sauced or pre-cooked)), tea, coffee, some sweets (galaxy, all types (crunchy, hazelnut, plain, almond, caramel and flutes (flutes with 1-pair and 2-pairs)), and we have grocery if you need.")
var choice = prompt ("Choose the item that you want.")
var addon = prompt ("Would you like any additional thing to your order?")
var amount = prompt ("How much do you want from the item you requested?")
var addonAmount = prompt ("How much additional items would you like for your order?")
switch (choice && addon && amount && addonamount) {
case 'chicken':
if (choice === "chicken" && addon === "no" && amount > 0 && addonAmount > 0) {
console.log("Okay. " + amount + " " + choice + "with " + addonAmount + addon + " coming right up!")
} else {
console.log("Sorry, but i didn't hear your request properly.")
}
case 'rice':
if (choice === rice && addon === "no" && amount > 0 && addonAmount > 0) {
console.log("Okay. " + amount + " " + choice + "with " + addonAmount + addon + " coming right up!")
} else {
console.log("Sorry, but i didn't hear your request properly.")
}
case 'tea':
if (choice === tea && addon === "no" && amount > 0 && addonAmount > 0) {
console.log("Okay. " + amount + " " + choice + "with " + addonAmount + addon + " coming right up!")
} else {
console.log("Sorry, but i didn't hear your request properly.")
}
default:
console.log("Sorry, but the items you requested was not found.")
}
*请注意代码仍有缺陷 编辑:我修改了代码,但它仍然存在缺陷,我需要帮助。
soo,代码中的错误产生线是什么,我该如何解决?谢谢。任何关于代码优化的建议都将不胜感激!
答案 0 :(得分:1)
有一些事情让我脱颖而出,帮助你摆脱你的错误。我不清楚你要为你的开关遵循什么逻辑,但我会指出你应该改变的一些事情,以避免你可能没有预料到的行为。
首先是在switch语句签名中有一个由&&分隔的系列变量运营商。这将导致最后一个变量被评估为您的“案例”。您的“案例”似乎是addonAmount
;但是看起来你可能只想要它choice
。你应该只将choice
放在签名中,因为如果这些值中的任何一个为false,它将只返回false。
它应该是这样的:
switch (choice) { ... }
(另外,你有一个错字:addonamount
应该是addonAmount
,但如果你只是把它拿出来就不再重要了)
其次,您需要确保在每个案例之后放置break;
,否则即使未满足条件,它也会运行下一个案例。
这应该是这样的:
case 'chicken':
if (choice === "chicken" && addon === "no" && amount > 0 && addonAmount > 0) {
console.log("Okay. " + amount + " " + choice + "with " + addonAmount + addon + " coming right up!")
} else {
console.log("Sorry, but i didn't hear your request properly.")
}
break; //make sure you do this at the end of every case block
case 'rice':
看起来您将茶作为变量引用而不是字符串:
if (choice === tea && addon === "no" && amount > 0 && addonAmount > 0) ...
您的错误是说未定义茶是因为您没有定义名为tea的变量。 tea
应该是一个字符串,而不是一个变量,这意味着你用引号括起来。当你在JavaScript中留下一系列没有引号的字母时,它会尝试将其解析为变量,除非它是一个特殊的JavaScript关键字(如function
,var
,switch
等)
请改为尝试:
if (choice === "tea" && addon === "no" && amount > 0 && addonAmount > 0) ...
此外,“米饭”选择也有同样的错误:
if (choice === rice && addon === "no" && amount > 0 && addonAmount > 0)...
应该是:
if (choice === 'rice' && addon === "no" && amount > 0 && addonAmount > 0)
有关switch语句的更多信息,请访问:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
如果您的代码仍然没有达到预期的效果,那么它可能位于if语句签名中,您正在执行一系列&&运营商再次。如果所有这些条件都返回真值,那么它只会进入if if块。
最后一点请注意,您应该选择引号。如果你想做单或双,只要你使用相同的(或者如果你在公司工作,它应该与代码库的其余部分相同)并不重要。
我希望这些变化可以帮助您更接近目标!