2015-07-21 41 views
0

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

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...'); 
} 
+2

你能描述的期望输出与实际输出? – MannfromReno

+0

唯一的输出我得到是: SyntaxError:意外标识符 – AZNPNOY2000

+0

您需要为yes和no定义变量,或者将th em在引号中(“是”,“否”)。在case语句之后,你还缺少一个右括号。 – nril

回答

1

你的吉他有一些问题。你需要声明两个变量的经验和兴趣,再加上你错过了一个。之前toLowerCase。你在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

看这句话在这里:

if (mInQuestion == yes) { 

您试图检查mInQuestion是字符串"yes",但你实际上是检查,看它是否等于变量yes不存在。把报价放在yesno附近。

还要注意这一行:

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

您不能检查这样的多个条件,你必须明确每个检查:

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

此外,无处不在,你试图调用toLowerCase是不正确。你正在试图调用一个函数,但是要离开括号。这意味着你的变量实际上是函数,而不是你想要的字符串。在每次调用后放置圆括号至toLowerCase

0

语法错误错过。这里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!"); 
    } 
0

三个问题:

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

  2. 除非变量yes在某处被声明,否则可能需要用引号括起来"yes"。类似地,对于noy(并且可能在某处)为n

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

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

    要将变量比较两个值,你独自做他们:

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

我是否必须用两个变量重复“variable == yes || variable == y”? – AZNPNOY2000

+0

@ AZNPNOY2000:是的,这就是我在上面做的原因。 –

-3

有几种错误/错误在你的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或空值之外的任何值),并且它将作为True传递。

+0

你的'if'条件逻辑仍然不正确。 'yes'和'y'是未定义的。 – mwilson

0

很多语法错误。你有一些变量,比如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...'); 
}