为什么我的代码返回ReferenceError:茶是没有定义,当我选择茶

why does my code return ReferenceError: tea is not defined when i choose tea?

本文关键字:定义 选择 代码 我的 返回 ReferenceError 为什么      更新时间:2023-09-26

我目前正在编写一些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块。

只是最后一个注意,你应该与你选择的引号一致。如果你想做单或双,没关系,只要你使用相同的(或者如果你在一个公司工作,它应该与其他代码库相同)。

我希望这些改变能帮助你更接近你的目标!