Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
¿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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!