2017-06-23 72 views
1

我做了一個石頭紙剪刀遊戲,但是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); 

其他一切工作正常,但它只是打印到控制檯。 幫助表示讚賞

回答

4

該程序無法訪問window.alert()調用。當程序遇到return語句時,執行返回到函數被調用的地方。您必須在每個return語句上移動window.alert()調用,以便在需要顯示警報時調用它們。

另外,使用「窗口」。在每個警報語句中都是可選的。

+0

謝謝,它的工作;但是,我將如何在沒有窗戶的情況下使用它?我需要聲明一個變量嗎?或者我可以把警報? (或.alert) – GlitchyShadowZ

+0

無需聲明變量。因爲window是一個全局對象,所以可以通過簡寫來調用alert()。請參閱此處的答案:https://stackoverflow.com/questions/13459907/what-is-the-difference-between-alert-and-window-alert。 – hRdCoder

1

除了一個,收益來自警報之前。一個返回打破了你的函數,該行的其餘部分不運行,嘗試切換它們各地

0

你缺少一些

else 

和一些大括號

,並請修復你的縮進。

0

警報無法訪問,因爲您在調用返回語句之前會返回該語句,這會退出該函數。而且你有一個缺失的大括號,它需要額外的其他內容,一旦我正確縮進代碼,這很容易被發現。

嘗試:

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); 
+0

此外,這是沒有window.alert工作,只需要警報。不需要變量。 –

相關問題