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
Why isn't my JavaScript Do-While Loop bringing the results I want?

This is the piece of code giving me headache. I am new to JavaScript and I am just learning the concepts your knowledge would do much help. The details of the code and output are given below. If you have an idea on how to solve this problem, please answer the question. Thanks!

//These are my variables
var candy= 10;
var cookies= 20;
var x= 20;
var y= 30;
var g= 1;

//While Loop
while (1 > 10){
    console.log ("x + y=50")
}//this won't run at all because 1<10 will never be true, I am 
trying to avoid infinity loops

while (g<=5) {
    console.log("The number is " + g )
    g++;
}/*if it was '<' it would be an infinity loop or we can just break 
the loop. 
output:
The number is 1
The number is 2
The number is 3.... All the way up to 5
*/

//Do-While Loop
do {
    console.log("This is a do-while loop")
} while(candy < 9 && cookies >= x)
/*This will execute once even though both the conditions are 
false, it's just how do-while loops were made*/


//This where the problem comes in
do{
    console.log("The other number is " + g)
    g++;
} while(g<=5)
/*output: The other number is 6
  I thought 6 is greater than 5? Why is it bringing such an output 
  yet the condition is g<=5?
  I was expecting:
  The other number is 1
  The other number is 2.... All the way up to 5
  */
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

let g = 1
while (g<=5) {
  console.log("The number is " + g )
  g++;
}
//Until g is 5 add one to g, this means that g is 6 here
console.log(g)

do{
  console.log("The other number is " + g)
  g++;
} while(g<=5)
// a do while loop executes once before the condition is checked
about 4 years ago · Juan Pablo Isaza Report

0

I checked your code and found issue.

while (g<=5) {
    console.log("The number is " + g )
    g++;
}

after this, the g is already 5 so when this come

do{
    console.log("The other number is " + g)
    g++;
} while(g<=5)

the loop will run only one time due to g is not below 5. if you change code like this. it will work well

var g=1;
do{
    console.log("g" + g);
    g++;
}while(g<=5)

just remove prev loop codes which make change g

about 4 years ago · Juan Pablo Isaza Report

0

In the first loop, where g is involved you check this condition: (g<=5). This means that the loop ends when g is greater than 5, right?

while (g<=5) {
    console.log("The number is " + g )
    g++;
}

Here you have

g=1 -> g++ -> g=2

g=2 -> g++ -> g=3

g=3 -> g++ -> g=4

g=4 -> g++ -> g=5

g=5 (g is still equals to 5, so your condition is met) -> g++ -> g=6

g=6 (g now is greater than 5, so your condition is not met. You don't loop anymore. Anyway g=6 because you incremented it with g++ in the previous iteration of the loop)

When you do the next loop, it is a do-while. This means that the condition is checked after the iteration.

do{
    console.log("The other number is " + g)
    g++;
} while(g<=5)

Having said this, what happens is:

You print "The other number is 6" because g is 6 since the last iteration of the previous loop

You increment g to 7

You check the condition: g=7 is minus or equals to 5? No. --> LOOP STOPS

You can try to debug and evaluate the values of g at each step.

Also, keep in mind that g is declared outside the loops, so its scope is "global". This means that the value won't "reset" at every new loop. That's why you don't get what you expect:

The other number is 1

The other number is 2.... All the way up to 5

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!