Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda