2016-11-27 80 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