Question: I need a Javascript function that will work with this code to show the winner of a rock paper scissors game. my game Rounds to

I need a Javascript function that will work with this code to show the winner of a rock paper scissors game.

my game

Rounds to Play: Rounds Remaining:

User Chose: Computer Chose:

JAVASCIPT:

function getElem(id) { return document.getElementById(id); }

var rounds;

function startGame() { rounds = getElem("ROUNDS_TO_PLAY"); rounds = parseInt(rounds.value);

document.roundsRemaining = rounds; getElem("ROUNDS_REMAINING").value = rounds; }

/* Responds to user choice button click. */ function userChoice(userButton) { updateStats(); displayUserChoice(userButton); var compChoice = getComputerChoice(); getElem("COMPUTER_CHOICE_OUTPUT").value = compChoice; displayComputerChoice(compChoice);

}

function updateStats() {

rounds=document.roundsRemaining; rounds--; document.roundsRemaining = rounds; getElem("ROUNDS_REMAINING").value = document.roundsRemaining;

if (document.roundsRemaining < 1) { alert("Game over"); getElem("ROCK_CHOICE").disabled=true; }

}

function getComputerChoice() { var r = Math.floor(Math.random() * 3)

switch (r) { case 0: return "ROCK"; case 1: return "PAPER"; case 2: return "SCISSORS"; default: console.log(r + " is not a valid computer choice."); }

}

function displayUserChoice(userButton) { var uco = getElem("USER_CHOICE_OUTPUT");

if (userButton == "ROCK") { uco.value = "ROCK"; } else if (userButton == "PAPER") { uco.value = "PAPER"; } else if (userButton == "SCISSORS") { uco.value = "SCISSORS"; } else { conosole.log(userButton + " is invalid!!"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!