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

250
Views
why does that while loop ignore it's != rule

So basically here is that problem I have from school about monsters and your energy that starts at the first index and each one after it is how much energy the fight with the monster will take for you to kill it. If you win 3 battles you get the count of battles added to your energy points. So I noticed that the End of Battle string gets through the while loop even though I put it in the condition with != :/

Im sorry im a bit of a noob..

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

You are sending in a string 'End of battle' and comparing that to 'End Of Battle'. They're not the same. To protect yourself against differences like this, you can normalize strings (trim off any excess white space and convert to lower case) before comparing. I adjusted this one line and it now works:

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!