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 trialidriss Elouilani
Courses Plus Student 1,232 PointsUSE THE FOR LOOP
Use for or while loop to iterate through the values in the temperatures array from the first item --100-- the last --10-- inside the loop.log the current array value to the console.
var temperatures = [100,90,99,80,70,65,30,10];
for (var i = 100; i<=100; i-=10){
console.log (temperatures[100]);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Damien Watson
27,419 PointsHi Idriss,
What the question is meaning is to loop from the first item in the array (item 0 => 100) to the last item in the array (item 7 => 10). Not to go from 100 to 10.
Starting with 'i' at the 0 position and incrementing until it hits the last position (temperatures.length). The output will also need to reflect the position (i) not (100)...
var temperatures = [100,90,99,80,70,65,30,10];
for (var i=0; i<=temperatures.length; i++){
console.log (temperatures[i]);
}
Your previous code would have output an error because you are starting i at 100 and continuing forever while i is less than or equal to 100, and then subtracting by 10. It would continue past 0 and on into the negatives.
The error occurs because you are trying to get position 100 from the array which only contains 8 items, otherwise you'd be caught in an endless loop.
var temperatures = [100,90,99,80,70,65,30,10];
for (var i = 100; i<=100; i-=10){
console.log (temperatures[100]);
}
Hope this helps.
idriss Elouilani
Courses Plus Student 1,232 PointsThank U Damien, God bless you for the logic. Idriss