0

Hi < 10 would be 9 & <=10 would be 10 and I know because the toptalLaps count starts from 0 the first loop print out 10 & 11 but the conditions are still <10 <=10 so I was wondering why the first loop is printing 10 times, & the second loop is printing 11 times?

var totalLaps = 0;

while (totalLaps < 10) {
   console.log('Swim Another lap!');
   totalLaps += 1;
}
**10** Swim Another lap!

var totalLaps = 0;

while (totalLaps <= 10) {
   console.log('Swim Another lap!');
   totalLaps += 1;
}
**11** Swim Another lap!

2 Answers 2

1

because you start count from 0 so totalLaps < 10 that mean the boucle Will start from 0 to 9 so is there 10 numbers

you can set totalLaps=1 then you can find 9 number between 1 and 9

var totalLaps=0;
while(totalLaps < 10){
  console.log("count : "+totalLaps);
    totalLaps +=1;
}
outupt

count : 0
count : 1
count : 2
count : 3
count : 4
count : 5
count : 6
count : 7
count : 8
count : 9

total = 10 numbers from 0 to 9
Sign up to request clarification or add additional context in comments.

Comments

0

Your first while loop has the condition totalLaps < 10, so it will continue to run as long as totalLaps stays under 10.

Your second while loop has the condition totalLaps <= 10. This means that the loop will continue to run as long as totalLaps is less than or equal to 10. The "equal to" part of the condition is causing your loop to run one extra time, and because your laps start at zero, this means your loop runs a total of 11 times.

Check out Wikipedia's article on Zero-based numbering, it might be of use to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.