Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

413
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!