2016-11-27 84 views
0

如何將我的搖滾,紙張,剪刀遊戲嵌入到按鈕中?將JavaScript嵌入按鈕

<!DOCTYPE html> 

<html> 


<button onclick="rockPaperScissors()">Click me</button> 



<script type="text/javascript"> 
var rockPaperScissors(
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) { 
    confirm("The result is a tie!";) 
    } 
    else if(choice1 === "rock") { 
    if (choice2 === "scissors") { 
     confirm("rock wins";) 
    } 
    else { 
     confirm("paper wins";) 
    } 
} 

//second one 

    else if(choice1 === "paper") { 
    if (choice2 === "rock") { 
     confirm("paper wins";) 
    } 
    else { 
     confirm("scissors wins";) 
    } 
} 

//third one 

    else if(choice1 === "scissors") { 
    if (choice2 === "rock") { 
     confirm("rock wins";) 
    } 
    else { 
     confirm("scissors wins";) 
    } 
} 
}; 

) 

</script> 

</html> 
+0

你沒有在'var rockPaperScissors('? – jediz

回答

0

還有更多的錯字在那裏......糾正代碼如下包括腳本var rockPaperScissors = function(){和確認的第一行應該總是看起來像confirm("paper wins");

<!DOCTYPE html> 
 

 
<html> 
 

 

 
<button onclick="rockPaperScissors()">Click me</button> 
 

 

 

 
<script type="text/javascript"> 
 
var rockPaperScissors = function(){ 
 
var userChoice = prompt("Do you choose rock, paper or scissors?"); 
 
var computerChoice = Math.random(); 
 
if (computerChoice < 0.34) { 
 
\t computerChoice = "rock"; 
 
} else if(computerChoice <= 0.67) { 
 
\t computerChoice = "paper"; 
 
} else { 
 
\t computerChoice = "scissors"; 
 
} console.log("Computer: " + computerChoice); 
 

 

 
var compare = function(choice1, choice2) { 
 
if (choice1 === choice2) { 
 
    confirm("The result is a tie!"); 
 
    } 
 
    else if(choice1 === "rock") { 
 
    if (choice2 === "scissors") { 
 
     confirm("rock wins"); 
 
    } 
 
    else { 
 
     confirm("paper wins"); 
 
    } 
 
} 
 

 
//second one 
 

 
    else if(choice1 === "paper") { 
 
    if (choice2 === "rock") { 
 
     confirm("paper wins"); 
 
    } 
 
    else { 
 
     confirm("scissors wins"); 
 
    } 
 
} 
 

 
//third one 
 

 
    else if(choice1 === "scissors") { 
 
    if (choice2 === "rock") { 
 
     confirm("rock wins"); 
 
    } 
 
    else { 
 
     confirm("scissors wins"); 
 
    } 
 
} 
 
}; 
 
}; 
 

 
</script> 
 

 
</html>

0

你的問題就在這裏:

var rockPaperScissors(

你需要rockPaperScissors是一個函數,而不是一個變量。

嘗試下面的代碼,以使rockPaperScissors是一個函數:

function rockPaperScissors() {}

var rockPaperScissors = function() {}

然後把你的相關代碼括號({ })內。

0

您還沒有正確聲明您的rockPaperScissors功能。所以,當程序試圖調用它時,它不起作用。

嘗試

function rockPaperScissors() { 
    // put your code in here 
} 
+0

這個代碼可能提出這個問題,提供關於如何解決問題和/或爲什麼解決問題的附加背景可以提高答案的長期價值。 – kayess