Jordan Iversen Jordan Iversen - 5 days ago 5
HTML Question

Embed JavaScript in a button

How can I embed my rock, paper, scissors game in a button?

<!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>

Answer

There are more typo in there... corrected code is below including the first line of the script var rockPaperScissors = function(){ and confirm should always look like 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) {
	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>