2016-06-12 48 views
-6

代碼沒有給予任何回報串

var userChoice = prompt("What would you like to play?"), 
 
    computerChoice = Math.random(); 
 
if (computerChoice <= 0.34){ 
 
    computerChoice = "Rock"; 
 
} 
 
else if (computerChoice >= 0.35 && computerChoice <= 0.67){ 
 
    computerChoice = "Paper"; 
 
} 
 
else { 
 
    computerChoice = "Scissors"; 
 
} 
 
document.write("Computer's choice is " + computerChoice); 
 
var compare = function(choice1, choice2) { 
 
    if (choice1 === choice2){ 
 
     return "This result is a tie!"; 
 
    } 
 
    else if (choice1 === "Rock") { 
 
     if (choice2 === "Scissors"){ 
 
      return "Rock Wins!"; 
 
     } 
 
    else { 
 
     return "Paper Wins!"; 
 
     } 
 
    } 
 
    else if (choice1 === "Paper"){ 
 
     if (choice2 === "Rock"){ 
 
      return "Paper Wins"; 
 
     } 
 
     else { 
 
      return "Scissors wins"; 
 
     } 
 
    } 
 
    else if (choice1 === "Scissors"){ 
 
     if (choice2 === "Rock"){ 
 
      return "Rock WINS"; 
 
     } 
 
     else { 
 
      return "Scissors wins"; 
 
     } 
 
    } 
 
}; 
 

 
document.write(compare(userChoice, computerChoice));
<!doctype html> 
 
<html> 
 
<title>JS Practice</title> 
 
<head> 
 
\t <script language="javascript"> 
 
</script> 
 
</head> 
 
<body> 
 
\t 
 
\t 
 

 
</body> 
 

 
</html>

請幫助我瞭解爲什麼它不顯示返回的字符串?當你贏或輸時,應該顯示結果。我從codeacademy得到了這段代碼。在他們的控制檯上工作完美,但是當你嘗試在瀏覽器上運行時,它不能正常工作。困惑......

var userChoice = prompt("What would you like to play?"); 
 
\t var computerChoice = Math.random(); 
 
\t if (computerChoice <= 0.34){ 
 
\t \t computerChoice = "Rock"; 
 
\t } 
 
\t else if (computerChoice >= 0.35 && computerChoice <= 0.67){ 
 
\t \t computerChoice = "Paper"; 
 
\t \t } 
 
\t else { 
 
\t \t computerChoice = "Scissors"; 
 
\t \t } \t 
 
\t document.write("Computer's choice is " + computerChoice); 
 
\t var compare = function(choice1, choice2){ 
 
\t \t if (choice1 === choice2){ 
 
\t \t \t return "This result is a tie!"; 
 
\t \t } 
 
\t \t else if (choice1 === "Rock") { 
 
\t \t \t if (choice2 === "Scissors"){ 
 
\t \t \t \t return "Rock Wins!"; 
 
\t \t \t } 
 
\t \t else { 
 
\t \t \t return "Paper Wins!"; 
 
\t \t \t } 
 
\t \t } 
 
\t \t else if (choice1 === "Paper"){ 
 
\t \t \t if (choice2 === "Rock"){ 
 
\t \t \t \t return "Paper Wins"; 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t return "Scissors wins"; 
 
\t \t \t } \t 
 
\t \t } 
 
\t \t else if (choice1 === "Scissors"){ 
 
\t \t \t if (choice2 === "Rock"){ 
 
\t \t \t \t return "Rock WINS"; 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t return "Scissors wins"; 
 
\t \t \t } 
 
\t \t } 
 
\t }; 
 
\t compare(userChoice, computerChoice);

+0

你如何在瀏覽器中運行它?在一個HTML頁面? – Netham

+0

YE。從記事本++我運行它在鉻。 – Marin

+0

請添加更多詳細信息:你的意思是說不能正常工作? – pietro909

回答

0

的代碼是否正確,工作正常,你忘記了顯示它的重要組成部分。

您不會將結果寫入實際文檔。你調用這個函數並且它正常返回,但是你沒有明顯地顯示出答案。試試這個代碼:

var userChoice = prompt("What would you like to play?"), 
 
    computerChoice = Math.random(); 
 
if (computerChoice <= 0.34){ 
 
    computerChoice = "Rock"; 
 
} 
 
else if (computerChoice >= 0.35 && computerChoice <= 0.67){ 
 
\t \t computerChoice = "Paper"; 
 
} 
 
else { 
 
\t computerChoice = "Scissors"; 
 
} \t 
 
document.write("Computer's choice is " + computerChoice); 
 
var compare = function(choice1, choice2) { 
 
\t if (choice1 === choice2){ 
 
\t \t return "This result is a tie!"; 
 
\t } 
 
\t else if (choice1 === "Rock") { 
 
\t \t if (choice2 === "Scissors"){ 
 
\t \t \t return "Rock Wins!"; 
 
\t \t } 
 
\t \t else { 
 
\t \t \t return "Paper Wins!"; 
 
\t \t } 
 
\t } 
 
\t else if (choice1 === "Paper"){ 
 
\t \t if (choice2 === "Rock"){ 
 
\t \t \t return "Paper Wins"; 
 
\t \t } 
 
\t \t else { 
 
\t \t \t return "Scissors wins"; 
 
\t \t } \t 
 
\t } 
 
\t else if (choice1 === "Scissors"){ 
 
\t \t if (choice2 === "Rock"){ 
 
\t \t \t return "Rock WINS"; 
 
\t \t } 
 
\t  else { 
 
\t \t \t return "Scissors wins"; 
 
\t \t } 
 
\t } 
 
    else { 
 
     return "Invalid input! Type either rock, paper, or scissors!" 
 
    } 
 
}; 
 

 
document.write(", " + compare(userChoice, computerChoice));

你失蹤的最後文件撰寫寫結果的文件。你可以自己格式化它。如果輸入無效,我還建議添加另一個案例。如果輸入無效,您也可以將主提示放入函數中並調用遞歸。

+0

它不起作用。它說Undefined ... – Marin

+0

@Marin如果你拼錯了,它可能會返回undefined,所以如果它不是有效的輸入,我會添加一個case – Li357

+0

這是所有的代碼。我會編輯它entierly。我也會添加html。 – Marin