Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda