这个JavaScript if / else语句有什么问题?

时间:2017-09-06 04:34:40

标签: javascript if-statement

所以我正在学习JavaScript,我只是在玩耍 - 在这里忍受我,我正在尝试学习一些概念,所以我正在一起攻击一个简单的“魔术8球”游戏。我想如果我使用math.random我可以生成一个随机数并使用if / else语句根据幕后生成的随机数显示“答案”。这个想法是让用户永远不会看到随机数。我的问题是......不管我做什么,脚本总是显示第一个if / else答案。我做错了什么?

这是我的codepen示例的链接供参考: https://codepen.io/celtninja/pen/NvmmNg

var answer = prompt("What is your question?");
var number = Math.floor(Math.random() * 20) + 1;
var realAnswer = number;


if(realAnswer = 20) {
    document.write("It is certain");
} else if (realAnswer = 19) {
    document.write("It is decidedly so");
} else if (realAnswer = 18) {
    document.write("Without a doubt");
} else if (realAnswer = 17) {
    document.write("Yes definitely");
} else if (realAnswer = 16) {
    document.write("You may rely on it");
} else if (realAnswer = 15) {
    document.write("As I see it, yes");
} else if (realAnswer =14) {
    document.write("Most likely");
} else if (realAnswer = 13) {
    document.write("Outlook good");
} else if (realAnswer =12) {
    document.write("Yes");
} else if (realAnswer = 11) {
    document.write("Signs point to yes");
} else if (realAnswer = 10) {
    document.write("Reply hazy try again");
} else if (realAnswer = 9) {
    document.write("Ask again later");
} else if (realAnswer = 8) {
    document.write("Better not tell you now");
} else if (realAnswer = 7) {
    document.write("Cannot predict now");
} else if (realAnswer = 6) {
    document.write("Concentrate and ask again");
} else if (realAnswer = 5) {
    document.write("Don't count on it");
} else if (realAnswer = 4) {
    document.write("My reply is no");
} else if (realAnswer = 3) {
    document.write("My sources say no");
} else if (realAnswer = 2) {
    document.write("Outlook not so good");
} else {
    document.write("Very doubtful");
}
<div id="demo"></div>

另外,一个附带问题,上面也是我如何计算1到20之间的随机数?

6 个答案:

答案 0 :(得分:5)

使用=realAnswer分配值。所以在你的代码中,比较按照下一个顺序进行

realAnswer = 20;
if (realAnswer) {
    // where comparision will return always true 
    // false will be only when realAnswer = 0
}

而是使用&#34;三倍相等&#34; - 确保值不相等而没有可能的类型转换 Comparison Operators (JavaScript)

if (realAnswer === 20) {
    // do something
}

在您的特定情况下Map将更具可读性和可维护性,然后ifswitch

const answers = new Map();
answers.set(20, "It is certain");
answers.set(19, "It is decidedly so");
answers.set(18, "Without a doubt");
// ... and so on

然后

const realAnswer = Math.floor(Math.random() * 20) + 1;
const answer = answers.get(realAnswer);

document.write(answer);

答案 1 :(得分:3)

if(realAnswer = 20)

=是一个赋值运算符

尝试==是等于运算符

if(realAnswer == 20){
    //do something
}

答案 2 :(得分:2)

if/else声明的问题在于,您使用的是=分配而不是==进行比较。 你应该把它改成:

if (realAnswer == 20) {
   . . .
}

但是,由于您对if使用了多个realAnswer语句,因此我强烈建议您改用switch/case。这样可以提高代码的可读性( 参见下面的代码段 ):

var answer = prompt("What is your question?");
var number = Math.floor(Math.random() * 20) + 1;
var realAnswer = number;

switch (realAnswer) {
  case 20:
    {
      document.write("It is certain");
      break;
    }
  case 19:
    {
      document.write("It is decidedly so");
      break;
    }
  case 18:
    {
      document.write("Without a doubt");
      break;
    }
  case 17:
    {
      document.write("Yes definitely");
      break
    }
  case 16:
    {
      document.write("You may rely on it");
      break;
    }
  case 15:
    {
      document.write("As I see it, yes");
      break;
    }
  case 14:
    {
      document.write("Most likely");
      break;
    }
  case 13:
    {
      document.write("Outlook good");
      break;
    }
  case 12:
    {
      document.write("Yes");
      break;
    }
  case 11:
    {
      document.write("Signs point to yes");
      break;
    }
  case 10:
    {
      document.write("Reply hazy try again");
      break;
    }
  case 9:
    {
      document.write("Ask again later");
      break;
    }
  case 8:
    {
      document.write("Better not tell you now");
      break;
    }
  case 7:
    {
      document.write("Cannot predict now");
      break;
    }
  case 6:
    {
      document.write("Concentrate and ask again");
      break;
    }
  case 5:
    {
      document.write("Don't count on it");
      break;
    }
  case 4:
    {
      document.write("My reply is no");
      break;
    }
  case 3:
    {
      document.write("My sources say no");
      break;
    }
  case 2:
    {
      document.write("Outlook not so good");
      break;
    }
  default:
    {
      document.write("Very doubtful");
      break;
    }
}
<div id="demo"></div>

此外,您的示例还有另一种方式。利用数组:

var answer = prompt("What is your question?");
var number = Math.floor(Math.random() * 20) + 1;
var realAnswer = number;

var answers = ['', "Very doubtful", "Outlook not so good", "My sources say no",
  "My reply is no", "Don't count on it", "Concentrate and ask again",
  "Cannot predict now", "Better not tell you now", "Ask again later",
  "Reply hazy try again", "Signs point to yes", "Yes", "Outlook good",
  "Most likely", "As I see it, yes", "You may rely on it", "Yes definitely",
  "Without a doubt", "It is decidedly so", "It is certain"
];

document.write(answers[realAnswer]);
<div id="demo"></div>

这个IMO拥有最少的代码。请记住,您需要将答案提供给数组,并将空字符串提供给数组的第一个元素。由于您从1-20生成随机数,因此可以使用。

答案 3 :(得分:0)

测试平等的最佳习惯是这样的:

if (20 === realAnswer)

而不是

if (realAnswer === 20)

然后,当您发出错误时

if (20 = realAnswer)

解释器/编译器会告诉您无法分配字面值20,并且您知道自己有问题。

防守代码!

答案 4 :(得分:0)

而不是嵌套的IF Else更好地使用Switch Case:

var answer = prompt("What is your question?");
var number = Math.floor(Math.random() * 20) + 1;
var realAnswer = number;

switch (realAnswer) {
    case 20:        
        document.write("It is certain");
        break;
    case 19:
        document.write("It is decidedly so");
        break;
    case 18:        
        document.write("It is decidedly so");
        break;
    ....
    ....
    ....
    ....
    ....
    ....
    ....
    ....


  default :{
      document.write("Very doubtful");
  }
}

答案 5 :(得分:0)

if(realAnswer = 20) {
    document.write("It is certain");
} 

这有什么不对,你是在分配价值而不是比较价值。请查看下面的javascript比较运算符或访问Comparison Operators

Operator Description               Comparing    Returns 
    ==      equal to                    x == 8       false  
                                        x == 5       true   
                                        x == "5"     true   

    ===     equal value and equal type  x === 5      true   
                                        x === "5"    false