JavaScript:这段代码有什么问题?

标签 javascript

  <html>
   <head>
    <script>
 var play;
 var target;
 var count = 0;
 var guess;
 var colors = ["blue ", "coral ", "orchid ", "red ", "cyan ", "dark orange", "aqua ", "yellow ", "gold ", "brown "];

 var colors = colors.sort();

 function do_this() {
   var choose = Math.floor(Math.random() * (colors.length));
   target = colors[choose];
   alert("The correct answer is " + target);
   alert(typeof(colors));
   while (true) {
     play = prompt("I am thinking one of these colors\n\n" + colors + "What color am I thinking of?");
     guess = colors.indexOf(play);

     count = count + 1;
     if (guess == -1) {
       alert("Sorry, I don't recognize your color");
       continue;
     }
     if (guess > target) {
       alert("Sorry, your guess is not correct\n\n" + "Hint: your color is alphabetically higher than mine\n\n Please try again");
       continue;
     }
     if (guess < target) {
       alert("Sorry, your guess is not correct\n\n" + "Hint: your color is alphabetically lower than mine\n\n Please try again");
       continue;
     }
     alert("Congratulations! You have guessed the color\n\n!" + "It took you" + count + "guesses to finish the game\n\n" +
       "You can see the color of the background");
     return true;
   }
 }

    </script>
    <body onload = "do_this()">
    </body>
    </head>
    </html>

当我尝试运行时,即使我输入了正确的颜色,它也会显示“抱歉,我无法识别您的颜色”,当我尝试检查它所采用的值时,猜测它显示的是 -1。请帮忙!

最佳答案

var color = ["蓝色"、"珊瑚色"、"兰花色"、"红色"、"青色"、"深橙色"、"水绿色"、"黄色"、"金色"、"棕色"];4

每个单词后面都有一个空格。去掉空格就可以了。另外4号又是怎么回事?您也可以删除它。

关于JavaScript:这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704559/

相关文章:

javascript - 如何在自定义字体 css 文件中查找可用的字体粗细

javascript - NodeJS - 使用 ajax 将数组从客户端传递到服务器

javascript - 在我的情况下如何强制 ipad 进入横向模式

javascript - Django 模板中的 moment.js 日期

javascript - 使用 Discord.js 检查嵌入中图像的 url

javascript - 在外部 Javascript 文件中使用 Ajax 将 Javascript 嵌套数组传递给 PHP

javascript - Firefox 中的网络音频

javascript - Handlebars 不会在我的 Backbone.js 集合上循环

Javascript - 如何访问 JSON 对象数组

javascript - Node js。未处理的 promise 拒绝