【问题标题】:javascript: if statement comparing string value not workingjavascript:如果比较字符串值的语句不起作用
【发布时间】:2016-08-18 03:10:31
【问题描述】:

我是 javascript 的新手,想知道为什么我的行在这里不起作用,情况如下:

我编写了一个非常简单的猜谜游戏,用户必须从单词列表中猜出随机生成的单词。

我想知道为什么代码没有比较用户的输入来查看是否来自列表?

提前谢谢你:)

var target_index;
  var guess_input_text;
  var finished=false;
  var number_of_guesses = 0;
  var colors = ["pink", 'olive', 'lime', 'orange', 'yellow', 'purple', 'indigo', 'blue', 'gray', 'black'];
  var guesses = 0;
  var color;


  function do_game() {
      var random_number = Math.random()*9;
      var random_number_intiger = Math.floor(random_number);
      target_index = random_number_intiger;
      color = colors[target_index];
      alert(color);

      while (!finished){
        guess_input_text = prompt('I am thinking of one of these colors.\n\n' + colors + '\n\n What do you think it is?');
        guesses++;
        alert(guess_input_text);
        finished = check_guess();
  }
}

  function check_guess(){
      if (guess_input_text===color){ //this works
        return true;
      }
      if (guess_input_text!=color && guess_input_text!=colors){  //why this is not working :(
        alert('Please choose your color among the colors listed below:\n\n' + colors);
        return false;
      }
      if (guess_input_text!=color && guess_input_text==colors){ //this is not working too
        alert('You are a bit off');
        return false;
      }
}

【问题讨论】:

  • 您不能将字符串guess_input_text 与数组colors 进行比较。它永远是不平等的。

标签: javascript arrays string if-statement compare


【解决方案1】:

我看到你在函数do_game()中丢失了'}'

function do_game() {
  var random_number = Math.random()*9;
  var random_number_intiger = Math.floor(random_number);
  target_index = random_number_intiger;
  color = colors[target_index];
  alert(color);

  while (!finished){
    guess_input_text = prompt('I am thinking of one of these colors.\n\n' + colors + '\n\n What do you think it is?');
    guesses++;
    alert(guess_input_text);
    finished = check_guess();
  }//Add here
}

变化:

if (guess_input_text!=color && guess_input_text!=colors)

if (guess_input_text!=color && guess_input_text==colors)

到:

if (guess_input_text!=color && colors.indexOf(guess_input_text) < 0)

if (guess_input_text!=color && colors.indexOf(guess_input_text) >= 0)

【讨论】:

  • 谢谢,但这不是问题!
【解决方案2】:

我看到的main 问题是您在guess_input_text!=colors 将字符串值与数组进行比较。这将始终评估为真,因为用户输入的字符串永远不会等于颜色数组。这意味着每当用户输入与随机颜色不匹配的颜色时,即使他们选择了数组中的颜色,他们也会得到“请在下面列出的颜色中选择您的颜色”。

您需要做的是检查用户输入的字符串是否存在于数组中。一种方法是改变

if (guess_input_text!=color && guess_input_text!=colors){  //why this is not working :(

if (guess_input_text!=color && colors.indexOf(guess_input_text)>=0){  //this will work if your browser/platform supports it :)

如果 indexOf 对您不可用,您可以通过其他方式执行此操作。

您的最后一个条件或多或少是不必要的,因为您已经知道颜色不等于随机颜色并且它在数组中。所以它可以被省略,你总是可以在那个时候发出你的最终警报命令。

【讨论】:

  • 感谢@Wake 的清晰解释,在我更换您的线路浏览器后无法运行!
  • @Ardavan,可能是您的浏览器不支持 indexOf,或者还有其他问题。有关 indexOf 的更多详细信息以及可以检查您的guess_input_text 字符串是否包含在您的颜色数组中的其他方法,请参阅此帖子:stackoverflow.com/questions/237104/…
猜你喜欢
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
相关资源
最近更新 更多