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

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

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 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