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 trialJamil Naeem
6,071 Pointshow do I use a for loop to cycle through an array full of numbers that do not have a consistent pattern
var temperatures = [100, 90, 99, 87, 45, 10] for (i=100; i<= temperatures.length; i++)
var temperatures = [100,90,99,80,70,65,30,10];
for (i = 100; i <= temperatures.length ; i += 1) {
console.log();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Ben Reynolds
35,170 PointsI assume you initialized i = 100 since that's the first value in the array, but you should start with the first index which is 0.
Since the length of the array is 8, the first time through the loop it will check if 100 <= 8, and never run. Try starting the counter at zero. Not sure exactly what you mean by numbers that do not have a consistent pattern, but the loop won't care what order they're in. In the console.log statement you can grab whatever item is at the index of i.
Jamil Naeem
6,071 PointsJamil Naeem
6,071 PointsThanks! I was a little confused about why it wasn't running but it was a syntax issue.