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, Arrays and Objects Tracking Multiple Items with Arrays Iterating through an Array

Why 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; }'''

script.js
var temperatures = [100,90,99,80,70,65,30,10];
var i = 0;
while (i<7){console.log(temperatures[i])
            i += 1;
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

6 Answers

It'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

Ooo, 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

The last element in the array isn't being output because your while loop stops too soon.

?? Its outputting stuff that isn't even in the array, im confused

When I run the code I see that. All the white numbers were logged from the while loop. The only one missing is 10

Yea I see that, isn't 20 also missing, and what is with all those other random numbers?

There 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.

AHAHA omg im so blind, my brain just thought there was a pattern, I didn't actually look

Thanks 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

Yeah that error didnt make sense to me.

You can mark this question as solved by selecting a "best answer". :smile:

Done, cheers Adam