Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops Working with 'for' Loops Refactor Code with a Loop

Why isn't my code logging the even numbers 2 through 24 to the console?

Hello, I'm absolutely stuck and can't figure out what's wrong with my code. Thank you for your help.

script.js
for ( let i = 2; i <= 24; i += 2 ) {
  console.log(`${i}`);
}
for(let i=2; i<=24; i++){
  if(i % 2 == 0){
      console.log(i);
        }
 }

The "if" statement will only even numbers because the % in JavaScript, divides a number and returns its remainder after the division.

ex)

2 / 2 = 0 , has no remainders , so it will meet the condition of "0" and will pass to the console

2 / 3 = has a remainder of 1 , so it doesn't meet the condition of 0 , so it will not be passed to the console

1 Answer

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,850 Points

Looks like it's testing that the output is a number, but the code you've written is outputting a string.