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 Create a for Loop

2 Answers

Chidiebere Ekennia
Chidiebere Ekennia
5,950 Points

Hello Heather.
There is an error in the logic you've used here.
In the conditional part of your code: (i <= 5 && i >= 100).

You have placed two different conditions as requirements for your code to keep running. It will return undefined or some other error because:

  1. You should one put only condition there ie: (i <= 5) OR (i >= 100).
  2. These conditions are logically incorrect and do not evaluate to true and hence cannot run.
  3. Your code is incrementing a variable named "counter" as opposed to "i" which you had defined earlier.

Why don't you tell me what you want to achieve and then i can tell you how to adjust your code to do it.

Chidiebere Ekennia
Chidiebere Ekennia
5,950 Points

Great news Heather. I took a look at the challenge and worked out how i think your code could've looked like. Here it is:

for (let i = 5; i<=100; i++) {
  console.log(i);
}

You will notice that:
1. There is only one condition in this for loop :

i<=100

2. The code evaluates to true as it increases the number 5 and will run while the new number is less than 100, just like in the condition i gave above.
3. I used the variable "i" instead of "counter" and so it means I am incrementing "i" instead of the variable "counter" that doesn't exist.

Heather Shore
seal-mask
.a{fill-rule:evenodd;}techdegree
Heather Shore
Full Stack JavaScript Techdegree Student 1,278 Points

Ohhhhhh. I didn’t even notice I had put counter in. Also. Turns out I like to make everything more difficult for myself! Thank you so much, that is way simpler than I tried to make it.

Chidiebere Ekennia
Chidiebere Ekennia
5,950 Points

You're welcome. Glad i could help. Don't forget to upvote the answer :)