Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda