Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

zkb
seal-mask
.a{fill-rule:evenodd;}techdegree
zkb
Front End Web Development Techdegree Student 7,529 Points

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}`);
}
Gremyko Coleman
Gremyko Coleman
9,265 Points
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,356 Points

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