Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                    
                
        

在警告时,第一个元素的索引即紫罗兰色显示为-1。但这对于获得正确的结果是出乎意料的。因此,我无法获得预期的结果,任何人都可以帮助我,以便我可以进一步进行编码。实际上m javascript的新功能,因此我正面临这样的问题。希望您能尽快回答我。



var colors = ["voilet", "indigo", "blue", "green", "yellow", "orange", "red"];
var target;
var target_index;
var guess_input;
var finished = false;

function do_game() {
  var random_number = Math.random() * 7;
  var random_number_integer = Math.floor(random_number);
  var target_index = random_number_integer;
  target = colors[target_index];
  alert(target);
  while (!finished) {

    var guess_input = prompt("I am thinking of a color " +
      "violet,indigo,blue,green,yellow,orange,red" +
      "What is the color?");
    alert(colors.indexOf(guess_input));
    finished = check_guess();
  }
}

function check_guess() {

  if (colors.indexof(guess_input) < 0) {
    alert('not present');
    return false;
  }
  if (guess_input > target) {
    alert('you gave large');
    return false;
  }
  if (guess_input < target) {
    alert('you gave small');
    return false;
  }
  return true;
}

最佳答案

您说您的guess_inputviolet,它返回-1

这是正常现象,因为数组中不存在violet

["voilet","indigo","blue","green","yellow","orange","red"];


出现错字错误:您写的是voilet而不是violet

09-18 13:10