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

120
Visualizações
Do-While loop output is undefined

the console shows undefined for the following loop. the issue is in array[i] as it says i is undefined but it is the index for the loop elements. I don't want to change the x to I as I want to run the code only once and test that it will stop as x is not < 5.

let x = 5;
let i;
let array = ["x", "y", "z"];
do {
  console.log(array[i]); //statement
  i++; //increment
} while (x < 5); //condition
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

so the problem with your code is that you initialised i without difining it let i; and then you are incrementing it i++, set let i = 0;

let x = 5;
let i = 0;
let array = ["x", "y", "z"];
do {
  console.log(array[i]); //statement
  i++; //increment
} while (x < 5); //condition

now if x is not less than 5 then the loop will run once but there will be a problem that if x is less than 5 then loop will run infinitely so instead of just x < 5 dot x < 5 && i < array.length

let x = 4;
let i = 0;
let array = ["x", "y", "z"];
do {
  console.log(array[i]); //statement
  i++; //increment
} while (x < 5 && i < array.length); //condition

now it will work just fine !

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