【问题标题】:If/Else statement only runs if code even if else condition is satisfiedIf/Else 语句仅在代码满足 else 条件时才运行
【发布时间】:2017-01-16 21:57:33
【问题描述】:

我正在尝试制作一个程序来谈论您最喜欢的科目以及您是右脑还是左脑。

function questionGame() {
var user = prompt("Are you RIGHT brained, LEFT brained, a bit of BOTH, or NEITHER?").toUpperCase();

switch (user){
 case 'RIGHT':
    var user1 = prompt("You like Art?").toUpperCase();
    if (user1 = 'YES' || 'YEAH') {
        alert("Sweet. You should check out LadyBeeSweets, she is the next michelangelo.");
    } else if (user1 = 'NO') {
        alert("Alright, ok, um, then do you like anything?");   
    } else {
        alert("I'm sorry, I don't understand " + user1 + ", please respond YES or NO next time, thank you!");
    };
     break;
 case 'LEFT':
     var user2 = prompt("Oh nice! Me too, I love my tech! Do you have a Linux-Based Operating System?").toUpperCase();
     if (user2 = 'YES') {
        alert("Sweet, me too!");   
     } else if (user2 = 'NO') {
        alert("Oh, you should definitely give one a try!")   
     } else {
       alert("I'm sorry, I don't understand " + user2 +", next time please respond with YES or NO.");  
     };
     break;
 case 'BOTH':
     var user3 = prompt("Interesting, so do you like Whales?").toUpperCase();
     if (user3 = 'YES' || 'YEAH') {
        alert("Cool."); 
     } else if (user3 = 'NO' || 'NOPE') {
        alert("You, MONSTER!"); 
     } else {
        alert("Hmm, whats that? I don't know if you like whales or not. Please reply YES or NO next time!"); 
     };
     break;
 case 'NEITHER':
     var user4 = prompt("Hmm, ok. I'm a left brainer! Do you care that I'm a Left Brainer? YES or NO?").toUpperCase();
     if (user4 = 'YES' || 'YEAH') {
        alert("Thanks for caring!");  
     } else if (user4 = 'NO' || 'NOPE') {
       alert("Wow, thanks a lot, bro.");   
     } else {
         alert("I couldn't understand " + user4 + ", please reply YES or NO next time, thank you");
     };
     break;
 default:
     alert("I'm sorry, I don't understand " + user + ", try again and chose either 'RIGHT', 'LEFT', 'BOTH', or 'NEITHER'.");
     questionGame();
     
    };
};

但是当您在第一个提示符中输入“Left”并询问您是否使用基于 linux 的操作系统时,即使您输入“no”或诸如“guhgriubcrhmiecrc”之类的随机输入,它仍然会运行第一个 if 语句代码块。 我感谢所有答案。顺便说一下,这是 JavaScript。

【问题讨论】:

  • user1 = 'YES' 不是比较运算,它是一个赋值,计算结果为真
  • 正如@PatrickEvans 所说,应该使用类似 if (user1 === 'YES' || user1 === 'YEAH') {
  • 成功了!谢谢你们,我应该知道这与 = 符号有关。

标签: javascript if-statement switch-statement prompt


【解决方案1】:

您正在使用赋值运算符 = 而不是比较运算符 ===== 在像 if (user2 = 'YES') 这样的语句中

【讨论】:

    【解决方案2】:

    正如 Jorg 所说,您是在分配一个变量,而不是比较它。既然您已经确定这是您所做的事情,那么通过将您的文字放在比较的左侧来避免将来这样做。

    if ('YES' === user2) {
            alert("Sweet, me too!");   
    }
    // If you forget to use multiple equals again, this code will throw an error and the problem will be more obvious
    if ('YES' = user2) {
            alert("Oops!");   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多