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

203
Views
The following javascript code gives output 6 . How the code is executed?

Why the output of the following code is not (1,2,3,4,5)?

var x = 0;

while (x < 6) {
  x++;
}

document.write(x);

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

0

That is because you are only writing to the document once.

For this, you should probably use a for loop.

for (let x = 0+1; x < 5+1; x++) {
  document.write(x);
}

This says x = 0 + 1 = 1 (so it starts at 0), then for every x > 5 + 1 (the +1 is so it ends at 5) do this, then add 1 to x.

Simply: x = 1, then when x is over 5, it will write to the document, then it will add 1 to x to continue the loop.

about 4 years ago · Juan Pablo Isaza Report

0

Each time the loop is repeated, the value in the x variable is updated. x++ increases the value of x by one unit And you write the final value

about 4 years ago · Juan Pablo Isaza Report

0

This example shows how to achieve this:

var x = 0;
let myArray = [];
while(x<6) {
  ++x; // We want x to start at 1
  myArray.push(x) // Add the new value of x into the array
}
const string = '(' + myArray.join(',') + ')'; // Format the output string by joining the array in a csv format and wrapping with brackets.
document.write(string);
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!