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

Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) me

How do I code this

script.js

Matthew Smart
Matthew Smart
12,567 Points

Have you actually attempted to do this?

1 Answer

Matthew Smart
Matthew Smart
12,567 Points

The answer to this would be:

*Answer removed  - You gave him explanation so let him have a try first.*

However you should first try and solve it yourself first, searching the internet for example how to do a javascript for loop would have took you to this page: http://www.w3schools.com/js/js_loop_for.asp

This shows you javascript for loops like the one used here.

To explain the loop:

  1. because you want to log the numbers 4-156 we will start with 4. That's why I make i = 4;
  2. i < 157; - this part is saying while 4 is less than 157
  3. i++ This means i = i + 1; So if i was 6 , it would become 7.

So every loop here will increment the variable i by 1. So inside the loop we use console.log(i); This will log the variable i in every loop.

because we start i at 4, and say while it's less than 157 it will log each number from 4 - 156 in your console.