簡體   English   中英

函數內的 JavaScript Alert 和 Prompt 未顯示在瀏覽器中

[英]JavaScript Alert and Prompt inside a function are not displaying in browser

/*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;
}

我改變了那里的一些東西,使這對你有用。

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();

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM