it's my first time on stack overflow and I have a MASSIVE question. I am looking to join a bootcamp and they are having me to a code challenge. I am at the last step of placing buttons on my HTML page that runs the rock paper scissors games but I cannot figure out how to link an HTML button to javascript function. Also, I don't know how to create a javascript function that runs as the HTML button is clicked. Please HELP!
<head>
<title>Lapis Papyrus Scalpellus</title>
</head>
<body>
<h1>Lapis Papyrus Scalpellus</h1>
<h2>Welcome to the ancient Showdown!!</h2>
<div>
<button id="Lapis">Lapis</button>
<button id="Papyrus">Papyrus</button>
<button id="Scalpellus">Scalpellus</button>
</div>
<h3>Pick your choice above!</h3>
<p id=results></p>
</body>
JAVASCRIPT
//Player's choice is everychanging so use let; USE OBJECT
let playerChoice = {
move: null,
}
//Computer's choice is everchanging so use let; USE OBJECT
let computerChoice = {
move: null,
}
document.querySelector(".Lapis").onclick = function(e) {
console.log(e.target);
}
//ONLY 3 choices so keep it constant; USE ARRAY;
const choices = ["Lapis", "Papyrus", "Scalpellus"];
playerChoice.move = choices[0];
//Runs the formula randomly through "choices" array and rounds down the answer; Below code is wrapped in computer choice function.
function computerChooses() {
const indexChoices = Math.floor(Math.random() * choices.length);
computerChoice.move = choices[indexChoices];
}
computerChooses();
//Changed console.log to document.write to see if it prints on HTML and it works.
function compareChoices(){
if (playerChoice.move === computerChoice.move) {
document.write("The game is a tie. Both players chose " + computerChoice.move + "!");
}else if (computerChoice.move === choices[2]){
if (playerChoice.move === choices[0]){
document.write("Player chose " + playerChoice.move + " and Computer chose " + computerChoice.move + ", Player Wins!");
}else {
document.write("Computer chose " + computerChoice.move + " and Player chose " + playerChoice.move + ", Whomp Whomp WHOMP Computer Wins!");
}
}else if (computerChoice.move === choices[0]){
if (playerChoice.move === choices[1] ){
document.write("Player chose " + playerChoice.move + " and Computer chose " + computerChoice.move + ", Whomp Whomp WHOMP Computer Wins!");
}else {
document.write("Computer chose " + computerChoice.move + " and Player chose " + playerChoice.move + ", Player Wins!");
}
}else if (computerChoice.move === choices[1]){
if (playerChoice.move === choices[2] ){
document.write("Player chose " + playerChoice.move + " and Computer chose " + computerChoice.move + ", Player Wins!");
}else{
document.write("Computer chose " + computerChoice.move + " and Player chose " + playerChoice.move + ", Whomp Whomp WHOMP Computer Wins!")
}
}
}
//called compareChoices function to let the game start
compareChoices();