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 trialBrittany Moyer
3,879 PointsCode Challenge Question
I'm not sure how to format this for loop since the numbers are not in order like normal. Could someone help me with this format?
My code:
for ( var i = 100; i < temperatures.length; i--[?]) console.log( i );
var temperatures = [100,90,99,80,70,65,30,10];
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Robert Richey
Courses Plus Student 16,352 PointsHi Brittany Moyer,
That's a really good attempt! Let's try a simple example that you can apply to the challenge. Please let me know if this helps or not.
Cheers
// let's iterate over an array of numbers and log out each value to the console
var numbers = [4, 3, 2, 1];
/*
remember that elements of an array start at index 0.
the variable i is your 'index' into the array
initialize i to 0, while i is less than the length of our numbers array,
increment i by 1 at the end of each loop iteration
*/
for (var i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// below is just pseudo code to help explain what is happening during the loop
Loop iteration 1:
i == 0
i < numbers.length // True
console.log(numbers[i]); // 4
i++
Loop iteration 2:
i == 1
i < numbers.length // True
console.log(numbers[i]); // 3
i++
Loop iteration 3:
i == 2
i < numbers.length // True
console.log(numbers[i]); // 2
i++
Loop iteration 4:
i == 3
i < numbers.length // True
console.log(numbers[i]); // 1
i++
Loop iteration 5:
i == 4
i < numbers.length // False
break out of loop
Brittany Moyer
3,879 PointsBrittany Moyer
3,879 PointsYou are a life saver. Thank you!