【发布时间】:2015-07-21 21:27:05
【问题描述】:
您好,我是 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...');
}
【问题讨论】:
-
你能描述一下预期输出和实际输出吗?
-
我得到的唯一输出是:SyntaxError: Unexpected identifier
-
您需要为 yes 和 no 定义变量,或者用引号将它们括起来(“yes”、“no”)。此外,您在 case 语句之后缺少一个右括号..
标签: javascript switch-statement conditional