gpt4 book ai didi

javascript - JavaScript 和编程新手。我的代码有错误吗?

转载 作者:行者123 更新时间:2023-12-03 09:29:50 31 4
gpt4 key购买 nike

嗨,我是 JavaScript 和一般编程的新手。我一直在使用 Codecademy 来帮助我学习。但是,我最近在 Codecademy 中创建的一组代码遇到了问题,但我似乎找不到问题所在。代码如下:

var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase;
if (mInQuestion == yes) {
var musInstrument = prompt('Which instrument do you play?').toLowerCase;
switch (musInstrument) {
case 'piano':
console.log('Oh great! Piano is wonderful!');
break;
case 'guitar':
console.log('Wow I love guitar as well!');
experience = prompt('Did you play when you were little?').toLowerCase;
interest = prompt('And do you like playing it?')toLowerCase;
if (experience && interest == yes||y) {
console.log("That's great! You must be a guitar master!");
}
else {
console.log("Keep learning! It will be great. Or you could do piano?");
}
break;
case 'flute':
console.log('Oh that\'s cool! You know I used to play flute as well!');
break;
default:
console.log('Oh I\'m not sure I\'ve heard of that instrument...sorry');
break;
}
else if (mInQuestion == no) {
console.log('Oh you don\'t? You should pick one like the piano or the guitar!');
}
else {
console.log('Your answer doesn\'t make sense...');
}

最佳答案

看这里的这一行:

if (mInQuestion == yes) {

您试图检查mInQuestion是否是字符串“yes”,但实际上您正在检查它是否等于变量yes 不存在。在 yesno 两边加引号。

另请注意这一行:

if (experience && interest == yes||y) {

您不能像这样检查多个条件,每次检查都必须明确:

if ((experience == 'yes' || experience == 'y') && (interest == 'yes' || interest == 'y'))

此外,您尝试调用 toLowerCase 的任何地方都是不正确的。您试图调用一个函数,但省略了括号。这意味着您的变量实际上是函数,而不是您想要的字符串。每次调用 toLowerCase

后添加括号

关于javascript - JavaScript 和编程新手。我的代码有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549920/

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