一些JavaScript代碼基於Codecademy的JavaScript軌道上的構建「搖滾,紙張,剪刀」課程。「剪刀」按鈕的作品,但「搖滾以及 「紙」 都顯示 「錯誤!」 的消息HTML/JavaScript搖滾,紙張,剪刀遊戲無法正常工作
HTML代碼:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1>Rock, Paper, Scissors</h1>
<button onClick='choose("rock")'>Rock</button>
<button onClick='choose("paper")'>Paper</button>
<button onClick='choose("scissors")'>Scissors</button><br>
<button onClick='compare(user, computerChoice)'>Go!</button>
<p id="result"></p>
</body>
</html>
Javascript代碼:
var user;
var choose = function(choice) {
user = choice;
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if (computerChoice < 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
document.getElementById("result").innerHTML = "Tie!";
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
document.getElementById("result").innerHTML = "You Win!";
}
else {
document.getElementById("result").innerHTML = "You Lose!";
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
document.getElementById("result").innerHTML = "You Win!";
}
else if (choice2 ==="scissors") {
document.getElementById("result").innerHTML = "You Lose!";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
document.getElementById("result").innerHTML = "You Lose!";
}
else if (choice2 === "paper") {
document.getElementById("result").innerHTML = "You Win!";
}
}
else {
document.getElementById("result").innerHTML = "ERROR!";
}
}
乍一看,看起來像你錯過了其他幾個... – 2014-02-12 21:05:07
@SakhalTurkaystan這就是解決方案。把它放在一個答案:-)這裏是它的小提琴:http://jsfiddle.net/5pxK6/ – Sirko
謝謝。我修正了錯誤,現在它完美地工作。 – nickrodriguez13