Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Odds/Evens Javascript Value not updating between turns

all the code was given by the teacher and you just had to place it in the right spot. even working with the teacher we couldn't get it to update the "value" attribute in between turns. it updates at the finish of the game but not during? what are we not seeing? any help appreciated.. if you need to see html i can add as comment

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

var getRandomNumber = function(max) {
var random;
if (!isNaN(max)) {
    random = Math.random(); //value >= 0.0 and < 1.0
    random = Math.floor(random * max); //value is an integer between 0 and max - 1
    random = random + 1; //value is an integer between 1 and max
}
return random;
};

var playGame = function() {
var odd = 0;
var even = 0;
var player, computer, total;
resetFields(); // clear any previous entries

while (odd < 50 && even < 50) {
    //get computers fingers
    computer = getRandomNumber(5);
    // get player's fingers
    player = parseInt(prompt("Enter a number between 1 and 5, or 999 to quit", 999));
    
    if (!isNaN(player) && player <= 5) {
        // show current round
        $("player").value = player;
        $("computer").value = computer;
        
        // update totals
        total = player + computer;
        if (total % 2 === 0) {
            even = even + total;
            $("even").value = even;
        } else {
            odd = odd + total;
            $("odd").value = odd;
        }
    }
    //if loop for player quitting
    if (player === 999) {
    resetFields();
    break;
    }
}
//after loop ends, determine winner
if (odd >= 50) { $("message").firstChild.nodeValue = "You WIN!"; }
else if (even >= 50) { $("message").firstChild.nodeValue = "You lose :("; }
else { $("message").firstChild.nodeValue = "You quit"; }

// set focus on button so you can play again
$("play").focus();
};

var resetFields = function() {
$("player").value = "0";
$("computer").value = "0";
$("odd").value = "0";
$("even").value = "0";
$("message").firstChild.nodeValue = "";
};

window.onload = function() {
$("play").onclick = playGame;
$("play").focus();
};

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!