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

248
Views
¿Por qué ese ciclo while ignora su regla !=

Así que básicamente aquí está el problema que tengo de la escuela sobre los monstruos y tu energía que comienza en el primer índice y cada uno después de eso es cuánta energía necesitarás para matarlo en la pelea con el monstruo. Si ganas 3 batallas, obtienes el recuento de batallas agregado a tus puntos de energía. Así que me di cuenta de que la cadena End of Battle pasa por el ciclo while a pesar de que la puse en la condición con != :/

lo siento soy un poco novato..

 function dude(input) { // inital energy as input(shift) let energy = Number(input.shift()); // we create a count and ++ it for every itteration let count = 0; // while input[0] is NOT "End Of Battle" and energy > 0 while (input[0] !== 'End Of Battle' && energy > 0) { // input.shift() is how much energy we will need for each monster let monster = Number(input.shift()); // we substract that input[0].shift() energy from the starting energy energy -= monster count++; if (count % 3 === 0) { energy += count; } console.log(energy); } if(energy <= 0){ console.log(`Not enough energy! Game ends with ${count} won battles and ${energy} energy`); }else{ console.log(`Won battles: ${count}. Energy left: ${Number(energy)}`); } } dude((["200", "54", "14", "28", "13", "End of battle"]));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está enviando una cadena 'Fin de la batalla' y comparándola con 'Fin de la batalla'. No son lo mismo. Para protegerse contra diferencias como esta, puede normalizar cadenas (recortar cualquier exceso de espacio en blanco y convertir a minúsculas) antes de comparar. Ajusté esta línea y ahora funciona:

 if (input[0].trim().toLowerCase() !== 'end of battle' && ...) 

 function dude(input) { // inital energy as input(shift) let energy = Number(input.shift()); // we create a count and ++ it for every itteration let count = 0; // while input[0] is NOT "End Of Battle" and energy > 0 while (input[0].trim().toLowerCase() !== 'end of battle' && energy > 0) { // input.shift() is how much energy we will need for each monster let monster = Number(input.shift()); // we substract that input[0].shift() energy from the starting energy energy -= monster count++; if (count % 3 === 0) { energy += count; } console.log('energy', energy); } if (energy <= 0) { console.log(`Not enough energy! Game ends with ${count} won battles and ${energy} energy`); } else { console.log(`Won battles: ${count}. Energy left: ${Number(energy)}`); } } dude(["200", "54", "14", "28", "13", "End of battle" ]);

about 4 years ago · Juan Pablo Isaza 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!