从技术上讲,我有一个链接导致我的脚本(对于缩进,如果不正确,很抱歉):
<!DOCTYPE html>
<html>
<title>Game</title>
<script>
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!";
}
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 === "paper"){
return "Scissors wins"
}
else {
return "Rock wins"
}
}
}
compare(userChoice, computerChoice);
</script>
<body>
<h1>The Rock Paper Scissors Game</h1><br>
<p>YAAAAY!</p>
</body>
</html>
所以,我的问题是我的脚本运行到var userChoice = prompt("Do you choose rock, paper or scissors?")
但是当我输入我的答案时,没有任何反应。
出了什么问题?!
buzzysin
答案 0 :(得分:2)
你的代码很好,你只需要做一些比较结果。像:
alert(compare(userChoice, computerChoice));
看到这个小提琴:https://jsfiddle.net/upcbdL6y/
答案 1 :(得分:1)
该函数返回结果,但您没有对结果做任何事情。此外,您需要将accept-charset
放在函数内,以便在每次运行时重新生成。这样你就可以在按下按钮而不是在加载时运行该功能。试试这个:
Math.random()
var userChoice = prompt("Do you choose rock, paper or scissors?");
function getCompChoice(userChoice) {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if (computerChoice <= 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
return compare(userChoice, computerChoice);
}
var compare = function(choice1, choice2){
if (choice1 === choice2){
return "The 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 === "paper"){
return "Scissors wins"
}
else {
return "Rock wins"
}
}
}
alert(getCompChoice(userChoice));