Javascript 和编程的新手。我的代码有误?

New to Javascript and Programming. Error with my code?

您好,我是 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

后加上括号

缺少语法错误。这里 interest = prompt('And do you like playing it?')toLowerCase;

 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!");
    }

你的吉他有一些问题。您需要声明两个变量 experience 和 interest,另外还缺少一个 .在小写之前。您还缺少 if 语句末尾的右括号 } 。如果你正在测试一个字符串,你的 if 语句也需要用引号格式化,并且每个条件都需要完整地写出来。

 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!');
            var experience = prompt('Did you play when you were little?').toLowerCase; //need to declare these
            var interest = prompt('And do you like playing it?').toLowerCase;
            if ((experience ==='yes' ||experience==='y') && (interest === 'yes'|| interest ==='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;
    }} //need bracket here
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...');
}

三个问题:

    上面的
  1. toLowerCase 应该是 toLowerCase() (到处都是)。前者是对函数的引用。后者 调用 函数和 returns 小写等效字符串。

  2. 除非您在某处声明了变量 yes,否则您可能希望将该字符串用引号引起来,例如"yes"。对于 noy(可能还有 n,某处)也是如此。

  3. 导致 Unexpected identifier 错误的错误在这里:

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

    要将一个变量与两个值进行比较,您可以分别进行:

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

你的js中有几个bugs/errors

这是其中之一:

    if (experience && interest == yes||y) {
        console.log("That's great! You must be a guitar master!");
    }

应该是

    if ( (experience == yes|| y ) && ( interest == yes||y) ) {
        console.log("That's great! You must be a guitar master!");
    }

因为之前的写法,(experience && interest == yes||y),experience 只需要有一个值(它可以是除 0 或 null 之外的任何值)并且它将作为 True 传递。

大量语法错误。您有一些变量正在比较提示中的输入,例如 mInQuestion == yes,这是不正确的(除非您有一个名为 yes 的变量。这需要更改为 mInQuestion == 'yes'。还有许多其他问题在您的代码中与此类似。此外,您还缺少第一个 if 语句中的右花括号 }。您还忘记调用某些函数,例如 toLowerCase() .

var mInQuestion = prompt('Do you play a musical instrument?').toLowerCase(); // <--forgot to invoke
if (mInQuestion == 'yes') { // <-- changed to 'yes'
    var musInstrument = prompt('Which instrument do you play?').toLowerCase(); // <--forgot to invoke
    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(); // <--forgot to invoke
            interest = prompt('And do you like playing it?').toLowerCase(); // <--forgot to invoke and forgot period
            if (experience && interest == 'yes' || 'y') { // <-- changed to 'yes' and '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;
    }
} // <--Added closing curly brace
else if (mInQuestion == 'no') { // <-- changed to '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...');
}