Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda