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

154
Vistas
Is there a way that I can console.log just once?

I'm relatively new to Javascript and I tried to code a text adventure game. What I am trying to do is when numLives == 1, I want the game to display "BE CAREFUL" just once when user chooses the wrong path, and if the user chooses the right path after that, even with numLives == 1, the message "BE CAREFUL" will not display anymore.

let game = ['a', 'b', 'b', 'a'];// correct answer sequence
let numLives = 4;
let validInput = ['a', 'b'];
let i = 0;
let r_idx = 0;
let w_idx = 0;
let right_narratives = [
  "You feel a gust of wind from the path on the right and hear growling on the path to the left.",
  "You see an energy star to collect to gain more energy",
  "You see bats hanging from the ceiling of the cave, but if you're quiet, you can pass through.",
  "A light is shining into your path. You follow it and find an exit to the cave."
];// right turn narratives

let wrong_narratives = [
  "You see a bear sleeping ahead. Careful not to wake him and go back where you were.",
  "This path is too narrow. Go back and try a different direction.",
  "There is a big boulder in the path. Go back and try a different direction.",
  "You didn't notice a hole in the cave and fell through."// wrong turn narratives
];
// if you want to add more narrative, please add more index to game[] for an extra step!
console.log("You have entered a cave, the entrance has collapsed behind you. You must venture forward to find the exit. Press 'a' to go left and 'b' to go right."); // introduction!
console.log("Please only enter a or b to play the game");
console.log("You will have 4 tries to finish the game");
while (true) {
    let inp = prompt("What's your move?");
    
  if (!validInput.find(x => x === inp)) {
    console.log("Wrong input, please try again");
    continue;
  }
  
  if (inp == game[i]) {
        i++; 
        if (i <= game.length) {
      console.log(right_narratives[r_idx]);
      r_idx++;
      console.log("Your move is correct");
    }
    } else {
    console.log(wrong_narratives[w_idx]);
    w_idx++;
        numLives--;
        console.log("Wrong move, you have ", numLives, " lives left")
    }
    if (numLives ==1){
        console.log("BE CAREFUL");
    }

    if (numLives == 0) {
        console.log("You lost!");
        break;
    }

    if (i >= game.length && numLives > 0) {
        console.log("You win!");
        break;
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You just need some basic conditional logic in there:

// declare this somewhere outside your loop
var warned = false;

Then check to see if they've been warned:

if (numLives == 1 && !warned) {
    console.log("BE CAREFUL");
    warned = true; // now they've been warned so this won't execute again
}
over 4 years ago · Santiago Trujillo 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