gpt4 book ai didi

JavaScript 切换函数

转载 作者:行者123 更新时间:2023-12-02 16:25:51 26 4
gpt4 key购买 nike

我正在尝试运行此代码,但它给我一个错误,提示“输入意外结束”。你愿意帮忙吗?提前致谢。

var getReview = function (movie) {
switch (movie) {
case "Toy Story 2":
return "Great story. Mean prospector.";
break;
case "Finding Nemo" :
return "Cool animation, and funny turtles.";
break;
case "The Lion King" :
return "Great songs.";
break;
default :
return "I dont know!";
break;
};

最佳答案

您的函数似乎缺少右大括号:

var getReview = function (movie) {
switch (movie) {
case "Toy Story 2":
return "Great story. Mean prospector.";
break;
case "Finding Nemo" :
return "Cool animation, and funny turtles.";
break;
case "The Lion King" :
return "Great songs.";
break;
default :
return "I dont know!";
break;
};
}; // <-- here

另请注意,在 return 后添加 break 或在 switch block 末尾添加分号是没有意义的。您可以将上面的内容简化为:

var getReview = function (movie) {
switch (movie) {
case "Toy Story 2":
return "Great story. Mean prospector.";
case "Finding Nemo" :
return "Cool animation, and funny turtles.";
case "The Lion King" :
return "Great songs.";
default :
return "I dont know!";
}
};

关于JavaScript 切换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28705577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com