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

261
Views
How to print even numbers using a for loop?

I find this simple "for loop" exercise.

Using a for loop print all even numbers up to and including n. Don’t include 0.

let n1 = 22; 

// Example output: 

// 2 4 6 8 10 12 14 16 18 20 22 OR each item on a new line

I found a way to solve this but I don't think its elegant at all.

Here is my code:

let n1 = 22;

for (let i = 0; i < n1; i++){
    
   let  b = i * 2;

    if (b <= n1){
        if (b == 0) {} else 
        { console.log('Count',b);}
    };
       
};

How can I improve it?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Just count by twos:

for (let i=2; i <= 22; i+=2) {
     console.log('Count', i);
}

about 4 years ago · Juan Pablo Isaza Report

0

for (let i=2; i <= 22; i+=2) {
     console.log(i);
}

all the best

about 4 years ago · Juan Pablo Isaza Report

0

Here using while loop:

const count = { index: 0, n1: 22 }

while(count.index < count.n1) {
  console.log('Count', count.index += 2)
}

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!