Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Javascript help on an odds and even game In this exercise, youll develop a player-vs-computer game that tallies odd and even numbers until theres a

Javascript help on an odds and even game

In this exercise, youll develop a player-vs-computer game that tallies odd and even numbers until theres a winner. You should design your interface to look similar to the image below (refer to the CSS and code used in the Reservation assignment to figure out the appropriate layout)

image text in transcribed

After the Play button is clicked, the user will see a series of prompts, like this:

image text in transcribed

The game ends when either the player or the computer reaches 50 or when the user enters 999 to quit. In either case, the application should display an appropriate message like: You WIN!, You lose :(, or You quit in the span element to the right of the Play button.

1. You will need five functions in your JavaScript file.

a. The $ function.

b. The getRandomNumber function.

c. A resetFields function that resets the text boxes and message area.

d. The start of a playGame function.

e. And an onload event handler that attaches the playGame function to the click event of the Play button. 2. Within the playGame function, use a while loop to get user entries until the player quits or either the player or computer has reached 50. Then, display the appropriate message to the right of the Play button. Within the while loop, consider using a break statement to end the loop if the player quits. 3. Within the playGame function, call the resetFields function whenever the fields need to be reset. 4. Be sure to have data validation to make sure the entry is a valid number from 0 through 5

HTML code:

Odds and Even Game

Let's Play ODDS AND EVENS!

Rules

  • The computer is even, you are odd (no offense).
  • Each round, you and the computer "hold up" 1 to 5 fingers.
  • The total number of fingers are tallied.
  • Even totals go to the computer.
  • Odd totals go to you.
  • First to 50 wins.

This Round:

Totals:

My javascript code, I know it needs some work. I have hidden things to test stuff out.

"use strict"; function $() { var $ = function(id) { return document.getElementById(id); }; }

var getRandomNumber = function(max) { var random;

if (!isNaN(max)) { random = Math.random(); //value >= 0.0 and

function resetFields() {

$("player").value = "0"; $("computer").value = "0"; //$("odd").value = "0"; //$("even").value = "0"; //$("message").firstChild.nodeValue = ""; }

function playGame(num) { var compTotal=0, userTotal=0, userNum=0, compNum=0, total=0; while(true) { userNum=prompt("Enter a number between 1 and 5, or enter 999 to quit."); if(userNum>=1 && userNum=50) { document.getElementById("message").innerHTML = "You win!"; break; //return false; //resetFields(); } else if(compTotal>=50) { document.getElementById("message").innerHTML = "You Lose!"; break; //return false; //resetFields(); } } else if(userNum==999) //if number entered is 999, the game quits. { document.getElementById("message").innerHTML = "You quit the game!"; return false; resetFields(); } else { alert("Please Enter a number between 1 and 5"); } } }

window.onload = function() {

$("play").onclick = playGame;

$("play").focus();

Let's Play ODDS AND EVENS! Rules- The computer is even, you are odd (no offense) Each round, you and the computer "hold up" 1 to 5 fingers The total number of fingers are tallied. Even totals go to the computer Odd totals go to you. First to 50 wins. Play This round You: 5 Computer: 2 Totals You: 16 Computer: 4 Enter a number between 1 and 5, or 999 to quit 999 OK Cancel Let's Play ODDS AND EVENS! Rules- The computer is even, you are odd (no offense) Each round, you and the computer "hold up" 1 to 5 fingers The total number of fingers are tallied. Even totals go to the computer Odd totals go to you. First to 50 wins. Play This round You: 5 Computer: 2 Totals You: 16 Computer: 4 Enter a number between 1 and 5, or 999 to quit 999 OK Cancel

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions