Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions

Question

3. Contrast relational contexts in organizations

Answered: 1 week ago

Question

2. Describe ways in which organizational culture is communicated

Answered: 1 week ago

Question

1. Describe and compare approaches to managing an organization

Answered: 1 week ago