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

412
Visualizações
while loop, JavaScript printing unexpected value

I have gone through this Question and Answer While loop inside while loop JavaScript

function getMaxLessThanK(n, k) {

  let i = 1;

  while (i < n) {
    let j = i + 1;
    while (j < n + 1) {
      console.log(i, j)
      j++
    }
    i++
  }
}

when n = 5, what am I getting is

1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
undefined

How to avoid this the last line undefined. what is the reason for it?

Can anyone help on this please?

Edit:

This is what the actual thing I am doing below.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you're running it in a console and get an undefined like this:

enter image description here

This undefined indicates that your statement runs without returning a value. It's not a result of your console.log.

If you make the function return something:

function getMaxLessThanK(n, k) {

  let i = 1;

  while (i < n) {
    let j = i + 1;
    while (j < n + 1) {
      console.log(i, j)
      j++
    }
    i++
  }
    return 'return value';
}

Then you'll get

enter image description here

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