【问题标题】:New to Javascript and Programming. Error with my code?Javascript 和编程新手。我的代码错误?
【发布时间】: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


【解决方案1】:

看这里的这一行:

if (mInQuestion == yes) {

您正在尝试检查mInQuestion 是否是字符串"yes",但实际上您是在检查它是否等于不存在的变量yes。在yesno 周围加上引号。

还要注意这一行:

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

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

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

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

【讨论】:

    【解决方案2】:

    您的吉他有一些问题。您需要声明经验和兴趣这两个变量,另外还缺少一个 .在小写之前。您的 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...');
    }
    

    【讨论】:

      【解决方案3】:

      语法错误遗漏。这里 interest = prompt('你喜欢玩吗?')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!");
          }
      

      【讨论】:

        【解决方案4】:

        三个问题:

        1. 上面的toLowerCase 应该是toLowerCase()(无处不在)。前者是对函数的引用。后者调用函数并返回等效的小写字符串。

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

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

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

          要将变量与两个值进行比较,请分别进行:

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

        【讨论】:

        • 我是否必须对两个变量重复“变量 == yes || 变量 == y”?
        • @AZNPNOY2000:是的,这就是我在上面做的原因。
        【解决方案5】:

        很多语法错误。您有变量正在比较提示中的输入,例如 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...');
        }
        

        【讨论】:

          【解决方案6】:

          您的 js 中有几个错误/错误。

          这是其中之一:

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

          因为它之前的编写方式,(经验 && 兴趣 == 是||y),经验只需要有一个值(它可以是除 0 或 null 之外的任何值)并且它将作为 True 传递。

          【讨论】:

          • 您的if 条件逻辑仍然不正确。 yesy 未定义。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-27
          • 2018-10-05
          • 2022-01-20
          • 1970-01-01
          • 1970-01-01
          • 2014-09-19
          • 1970-01-01
          相关资源
          最近更新 更多