为什么我的代码返回ReferenceError:茶是没有定义,当我选择茶
why does my code return ReferenceError: tea is not defined when i choose tea?
我目前正在编写一些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.")
}
*注意代码仍然有缺陷编辑:我修改了代码,但它仍然有缺陷,我需要帮助。
那么,代码中的错误行是什么,我该如何修复它?谢谢。任何关于代码优化的建议将不胜感激!
我认为有一些方法可以帮助你摆脱bug。我不清楚你想要遵循什么逻辑来切换,但我会指出一些你应该改变的东西,以避免你可能意想不到的行为。
第一件事是在switch语句签名中有一系列变量用&&操作符。这将导致最后一个被评估的变量成为您的"case"。看起来你的"案例"将是addonAmount
;然而,看起来你可能只想要它是choice
。您应该将choice
单独放在签名中,因为如果这些值中的任何一个为false,它将只返回false。
应该是这样的:
switch (choice) { ... }
(还有,你有一个错别字:addonamount
应该是addonAmount
,但如果你只是把它去掉,那就不再重要了)
其次,您需要确保在每个case后面放置break;
,否则即使没有满足标准,它也会运行下一个case。
应该是这样的:
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':
看起来你把tea引用为一个变量,而不是一个字符串:
if (choice === tea && addon === "no" && amount > 0 && addonAmount > 0) ...
你的错误是说tea没有定义,因为你没有定义一个名为tea的变量。tea
应该是一个字符串,而不是一个变量,这意味着你要用引号把它括起来。当你在JavaScript中留下一系列没有引号的字母时,它会尝试将其解析为变量,除非它是一个特殊的JavaScript关键字(如function
, var
, switch
等)
试试这个:
if (choice === "tea" && addon === "no" && amount > 0 && addonAmount > 0) ...
同样,选择"rice"也有同样的错误:
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块。
只是最后一个注意,你应该与你选择的引号一致。如果你想做单或双,没关系,只要你使用相同的(或者如果你在一个公司工作,它应该与其他代码库相同)。
我希望这些改变能帮助你更接近你的目标!- 自定义选择框-选择选项时占位符中的数据图标未正确更新
- drupal 中的自定义选择框
- 为什么我不能使用自定义选择的属性
- 如何使用自定义选择框
- Foundation 4 Forms JavaScript:禁用自定义<选择>
- 自定义选择的 jQuery
- WooCommerce - 使用自定义选择框触发变体选择
- 需要 NicEditor 上的解决方案 - 如何添加自定义选择选项
- 如何在 AngularJS 中定义选择选项值
- 自定义选择和输入标签,IE版本看起来很糟糕
- 如何在android webview中为所有版本自定义选择自定义栏
- 如何使用CasperJS与自定义选择框交互
- IE8的自定义选择框
- 自定义选择选项作为mysql填充的下拉列表中的第一个选项
- 自定义选择功能,复制到剪贴板纯JS
- 自定义选择下拉,无法选择用户添加的项目
- 自定义选择框的表索引问题
- jQuery Mobile:在自定义选择结束时添加关闭按钮
- 使用自定义选择器和逻辑进行验证
- 删除自定义选择绑定器与更新功能