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 trialNick Evershed
6,429 PointsWhy is my code not working?
javascript'''var temperatures = [100,90,99,80,70,65,30,10]; var i = 0; while (i<7){console.log(temperatures[i]) i += 1; }'''
var temperatures = [100,90,99,80,70,65,30,10];
var i = 0;
while (i<7){console.log(temperatures[i])
i += 1;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
6 Answers
Adam N
70,280 PointsIt's easier to see what is going wrong by running the code here: https://repl.it/repls/EssentialSelfreliantLock
Let me know if this helps here
Nick Evershed
6,429 Points?? Its outputting stuff that isn't even in the array, im confused
Adam N
70,280 PointsWhen I run the code I see that. All the white numbers were logged from the while loop. The only one missing is 10
Nick Evershed
6,429 PointsYea I see that, isn't 20 also missing, and what is with all those other random numbers?
Adam N
70,280 PointsThere is no number 20 in the array.
var temperatures = [100,90,99,80,70,65,30,10];
I'd ignore the number 7 that's being returned at the end. repl.it is returning the value of i at the end of the script for whatever reason.
Nick Evershed
6,429 PointsAHAHA omg im so blind, my brain just thought there was a pattern, I didn't actually look
Nick Evershed
6,429 PointsThanks man, I fixed it, although there was a weird error when I just entered one number below for the loop to run, it was something like a typeError it confused me
Adam N
70,280 PointsYeah that error didnt make sense to me.
You can mark this question as solved by selecting a "best answer".
Nick Evershed
6,429 PointsDone, cheers Adam
Nick Evershed
6,429 PointsNick Evershed
6,429 PointsOoo, nice resource, but I already used a console to check what it was outputting, im really confused why it is outputting these things, i've tried to visualise the logic, but it just makes no sense
Adam N
70,280 PointsAdam N
70,280 PointsThe last element in the array isn't being output because your while loop stops too soon.