【问题标题】:JavaScript Alert and Prompt inside a function are not displaying in browser函数内的 JavaScript 警报和提示未在浏览器中显示
【发布时间】:2016-02-08 07:13:14
【问题描述】:
/*Below is a color guessing game that is supposed to run in browser but when I open the code below in my Chrome, nothing happens. Any ideas? I would appreciate any help.*/

/*Below is a color guessing game that is supposed to run in browser but when I open the code below in my Chrome, nothing happens. Any ideas? I would appreciate any help.*/

var colors = ["Aqua", "Black", "Blue", "Brown", "Coral", "Crimson", "Cyan","Fuchsia", "Gold", "Gray", "Green"];
var target;
var guess_input_text;
var guess_input;
var finished = false;
var guesses = 0;

function do_game() {
    var random_number = Math.random() * colors.length - 1;
    var target_index = Math.floor(random_number);
    target = String(colors[target_index]);

    //what is wrong below? Why is this not working in my Chrome browser.

    while (!finished) {
      guess_input_text = prompt("I am thinking of these colors:\n\n"
      "Aqua, Black, Blue, Brown, Coral, Crimson, Cyan, Fuchsia, Gold, Gray, Green\n\n"
      "What color am I thinking of?");
      guess_input = String(guess_input_text);
      guesses += 1;
      finished = check_guess();
    }
}

/* Is my first if statement correct? Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?Is my first if statement correct?*/

function check_guess(){

    if (guess_input != "Aqua"||"Black"||"Blue"||"Brown"||"Coral"||"Crimson"||"Cyan"||"Fuchsia"||"Gold"||"Gray", "Green") {
      alert("Sorry, I don’t recognize your color.\n\nPlease try again.");
    }

    if(guess_input < target){
      alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically higher than mine.");
      return false;
    }

    if(guess_input > target){
      alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically higher than mine.");
      return false;
    }

    alert("Congratulations! You have guessed the color!\n\n"
    "It took you " + guesses + " to finish the game!\n\n"
    "You can see the color in the background.");
    return true;
}

【问题讨论】:

  • 请不要(试图)在 cmets 中描述您的问题。在代码之外写一个适当的描述。一遍又一遍地重复同样的句子也没什么用。
  • 有什么问题?
  • if(guess_input != "Aqua"||"Black"||"Blue"||"Brown"||"Coral"||"Crimson"||"Cyan"||"Fuchsia"||"Gold"||"Gray", "Green") 将始终评估为 true 因为整个事情将评估为 "Green" 这是真实的。换句话说,您的 if 语句在语义上是不正确的。
  • 您的代码有几个部分在语义或句法上不正确。请查看控制台是否有错误并使用 JSHint。

标签: javascript arrays loops while-loop


【解决方案1】:

我在其中更改了一些内容以使其适合您。

var colors = ["Aqua", "Black", "Blue", "Brown", "Coral", "Crimson", "Cyan","Fuchsia", "Gold", "Gray", "Green"],
    colorsText = colors.join(', '),
    target,
    guess,
    guesses = 0,
    tries_allowed = 10;

function play_game() {
    var random_number = Math.random() * (colors.length - 1);
    target = Math.floor(random_number);

    make_guess();
    return;
}
function make_guess(){
      guess = prompt("I am thinking of one of these colors:\n\n" + colorsText + "\n\Which color is it?");
      guesses++;
    if (!(check_guess(guess)) && guesses < tries_allowed) make_guess();
    return;
}

function check_guess(guess){
    var guess_index = colors.indexOf(guess);
    if(guess_index === target){
        alert("Congratulations! You have guessed the color - " + colors[target] + "!\n\nIt took you " + guesses + " guesses to finish the game!\n\nYou can see the color in the background.");
        return true;
    }
    if (guess_index <= -1) {
      alert("Sorry, I don’t recognize your color.\n\nPlease try again.");
      return;
    }
    var hint;
    if(guess_index < target) hint = 'lower';
    if(guess_index > target) hint = 'higher';
    alert("Sorry, your guess is not correct!\n\nHint: your color is alphabetically " + hint + " than mine.");
    return;
}

play_game();

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 2018-11-09
    • 2023-02-05
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多