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

172
Visualizações
¿Hay alguna manera de que pueda console.log solo una vez?

Soy relativamente nuevo en Javascript y traté de codificar un juego de aventuras de texto. Lo que estoy tratando de hacer es cuando numLives == 1 , quiero que el juego muestre "TENGA CUIDADO" solo una vez cuando el usuario elige el camino incorrecto, y si el usuario elige el camino correcto después de eso, incluso con numLives == 1 , el mensaje "TENGA CUIDADO" ya no aparecerá.

 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

Solo necesitas algo de lógica condicional básica allí:

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

Luego verifique si han sido advertidos:

 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