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

123
Vistas
Javascript guessing game with cookie

Instruction:

Create a number guessing game to generate a number between the range of 1 and 2. The game should prompt users for their username (saved in cookie). Set range as function parameter and prompt the player to predict the generated number between the given range, at a correct guess, the game should award the player a point (also saved in cookie), and move them to stage 2 by increasing the range limit value by 1, e.g range is from 1 and 3 for stage 2 and so on.

This is what I have done so far.

playButton.addEventListener("click", () => {
  const guessingGame = (range) => {
    const rndGuess = Math.floor(Math.random() * range) + 1;
    console.log(rndGuess);
    let point = 0;
    const playerName = prompt("Please enter username");
    setCookie("username", playerName, 1);
    const guess = prompt(`Predict a number between 1 and ${range}`);
    const playerGuess = Number(guess);

    if (rndGuess === playerGuess) {
      console.log(point, "point");
      point = point + 1;
      console.log(point);
      alert(`You won!, ${playerName}, You've scored ${point} point`);
      setCookie("point", point, 1);
      guessingGame(range + 1);
      point = point += 1;
      setCookie("point", point, 1);
    } else {
      point = 0;
      alert("You lose!");
    }
  };
  guessingGame(2);
});

problem: point does not increase, and I only want to prompt username once if rndGuess === playerGuess is true.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I got it working this way

playButton.addEventListener("click", () => {
  const playerName = prompt("Please enter username");
  setCookie("username", playerName, 1);
  let point = 0;
  const guessingGame = (range) => {
    const rndGuess = Math.floor(Math.random() * range) + 1;
    console.log(rndGuess);
    const guess = prompt(`Predict a number between 1 and ${range}`);
    const playerGuess = Number(guess);

    if (rndGuess === playerGuess) {
      point = point + 1;
      setCookie("point", point, 1);
      alert(`You won!, ${playerName}, You've scored ${point} point`);
      guessingGame(range + 1);
    } else {
      point = 0;
      alert("You lose!");
    }
  };
  guessingGame(2);
});
about 4 years ago · Juan Pablo Isaza Denunciar
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