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 trialHeather Shore
Full Stack JavaScript Techdegree Student 1,278 Pointsfor loop
What am I doing wrong here?
for (let i = 5; i <= 5 && i >= 100; counter ++ ) {
console.log(i);
}
2 Answers
Chidiebere Ekennia
5,950 PointsHello 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:
- You should one put only condition there ie: (i <= 5) OR (i >= 100).
- These conditions are logically incorrect and do not evaluate to true and hence cannot run.
- 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
5,950 PointsYou're welcome. Glad i could help. Don't forget to upvote the answer :)
Chidiebere Ekennia
5,950 PointsChidiebere Ekennia
5,950 PointsGreat news Heather. I took a look at the challenge and worked out how i think your code could've looked like. Here it is:
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
Full Stack JavaScript Techdegree Student 1,278 PointsHeather Shore
Full Stack JavaScript Techdegree Student 1,278 PointsOhhhhhh. 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.