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, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

Trying to select numbers 4 to 156 in this code challenge. No idea how to do it.

I learned how to select numbers from 1 to 10, but I'm stumped when it comes to selecting a specific set of numbers in between?

script.js
for (var i = 1; i <= 156, i >= 4; i += 1){
    console.log ()
}

1 Answer

Matthew Lanin
seal-mask
.a{fill-rule:evenodd;}techdegree
Matthew Lanin
Full Stack JavaScript Techdegree Student 8,003 Points

Since i is going to be the variable that we increase and that counts our numbers between, 4 and 156, we put i in the parenthesis of the console.log() statement.

  for (var i = 1; i <= 156, i >= 4; i += 1) {
    console.log(i);
  }

Also, since we know we're starting at 4, we can just initialize i to 4, and remove the i >= 4 portion of the for loop.

  for (var i = 4; i <= 156; i += 1) {
    console.log(i);
  }

So with that, we log to the console everything between (and including) 4 and 156.