我做了一個石頭紙剪刀遊戲,但是window.alert不工作,所以用戶看不到他們是否贏了!我將如何解決這個問題? 這裏是我的JS:rock paper scissors window.alert不能正常工作
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);
其他一切工作正常,但它只是打印到控制檯。 幫助表示讚賞
謝謝,它的工作;但是,我將如何在沒有窗戶的情況下使用它?我需要聲明一個變量嗎?或者我可以把警報? (或.alert) – GlitchyShadowZ
無需聲明變量。因爲window是一個全局對象,所以可以通過簡寫來調用alert()。請參閱此處的答案:https://stackoverflow.com/questions/13459907/what-is-the-difference-between-alert-and-window-alert。 – hRdCoder