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 trialSudhir Kumar
Courses Plus Student 925 PointsCan you help me with this code
Is this code correct or I type it wrong
var temperatures = [100,90,99,80,70,65,30,10];
for (var i = 0; i < temperatures.length; i += 1) {
console.log(temperatures);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Sudhir Kumar
Courses Plus Student 925 Pointsplease click on the view challenge on the top right of the page
Anton Williams
5,157 Pointsconsole.log(temperatures[i]);
if you want to loop through the temperatures this is what you'd do
3 Answers
Steven Parker
231,269 PointsThe challenge wants only one item logged at a time.
Your code logs the entire array each time through the loop. Try selecting just one of the items to log:
console.log(temperatures[i]);
Sudhir Kumar
Courses Plus Student 925 Pointsthanks for helping me out
Erik Nuber
20,629 PointsThe way it is written right now, it is simply logging the entire array to the console 8 times. To fix it so that it puts each temperature individually you would change this statement
console.log(temperatures[i]);
That is if what you are trying to achieve is an iteration thru the array itself.
Sudhir Kumar
Courses Plus Student 925 Pointsthanks
Jason Anders
Treehouse Moderator 145,860 PointsHey Sudhir,
Your really close. In the console log, the code is just logging temperatures as the entire array. Using the i
variable from the loop, you need to log each item in the array separately.
So, it's just one little change you need to make to log out each array item as the loop moves forward.
for (var i = 0; i < temperatures.length; i += 1) {
console.log(temperatures[i]);
}
Notice the [i]
appended to the end the temperatures value. This now takes the value of i
and calls the appropriate item in the array. I hope that helps.
jacinator
11,936 Pointsjacinator
11,936 PointsWhat is your error?