摇滚纸剪刀window.alert不工作

时间:2017-06-23 14:17:12

标签: javascript html html5

我做了一个石头剪刀游戏,但window.alert不能正常工作,所以用户无法看到他们是否赢了!我该如何解决这个问题? 这是我的JS:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
 if(choice1 === choice2) {
    return "The result is a tie!";
      window.alert("Wait what. We tied?");
    }

    else if(choice1 === "rock") {

        if(choice2 === "scissors") {
            return "rock wins";
              window.alert("What? Why u crush my scissors :<. I guess you won!");
        }
        else {
            return "paper wins";
            window.alert("Got a present for you! Just kidding lol its a rock packed in paper. Imma throw it at you and you will die.");
        }
    }
    if(choice1 === "paper")
        if(choice2 ==="rock"){
            return "paper wins";
            window.alert("Your paper, Vs my Rock! Hah! I won!");
        }
    if(choice1 === "scissors"){
        if(choice2 ==="rock"){
            return "rock wins";
              window.alert("I just crushed your scissors fam. I won. ezpz take the L");
        }
    else{
        return "scissors wins";
          window.alert("You spooked m8? My scissors cut you in 326 pieces! >:D");
         }
    }
};
compare(userChoice,computerChoice);

其他一切正常,但它只是打印到控制台。 感谢帮助

4 个答案:

答案 0 :(得分:4)

程序无法访问window.alert()调用。当程序遇到return语句时,执行返回到调用函数的位置。您必须在每个return语句上方移动window.alert()调用,以便在您希望显示警报时调用它们。

另外,使用&#34;窗口。&#34;在每个警报声明中都是可选的。

答案 1 :(得分:1)

除了一个,回复在警报之前。返回会使您退出某个函数,其余行不会运行,请尝试将它们全部切换

答案 2 :(得分:0)

你错过了一些

else

和一些花括号

请修改你的缩进._。

答案 3 :(得分:0)

警报无法访问,因为在调用它们之前有一个return语句,退出该函数。你有一个缺少大括号,它需要一个额外的其他,这很容易发现一旦我正确缩进代码。

尝试:

var computerChoice = Math.random();

if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if(computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
  if(choice1 === choice2) {
    return "The result is a tie!";
    window.alert("Wait what. We tied?");
  } else {
    if(choice1 === "rock") {
      if(choice2 === "scissors") {
        alert("What? Why u crush my scissors :<. I guess you won!");
        return "rock wins";
      } else {
        alert("Got a present for you! Just kidding lol its a rock packed in paper. Imma throw it at you and you will die.");
        return "paper wins";

      }
    }

    if(choice1 === "paper") {
      if(choice2 ==="rock") {
        alert("Your paper, Vs my Rock! Hah! I won!");
        return "paper wins";  
      }
    }

    if (choice1 === "scissors") {
      if(choice2 ==="rock"){
        alert("I just crushed your scissors fam. I won. ezpz take the L");
        return "rock wins";  
      } else {
        return "scissors wins";
      }
    }
};
compare(userChoice,computerChoice);