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

Derek Hutson
Derek Hutson
9,740 Points

How do I subtract different numbers?

Hi all,

How do I subtract different amounts from an array list? Almost there with for loop but can't figure out what last -= is...

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

It doesnt want you to subtract. It wants you to iterate through the array using a loop.

var temperatures = [100,90,99,80,70,65,30,10]; var temp;

for(i=0; i < temperatures.length; i+=1){ temp = temperatures[i]; console.log(temp); }

2 Answers

Steven Parker
Steven Parker
231,275 Points

The objective of the challenge is not to subtract from the list values, but just to output each one to the console.

When constructing your loop, remember that your loop variable only needs to keep track of which item you are currently logging, so it can serve as the array index. So it should start with 0 and stop when it gets to the size of the list. And it should not skip any values in between.

You're not subtracting anything, you need to iterate through the values in the array and log the current value to the console.

var temperatures = [100,90,99,80,70,65,30,10]; 
for (let i=0; i < temperatures.length; i += 1) {
 currentTemp = temperatures[i]; 
console.log(currentTemp); 
}

As long as "i" is less then the number of values in the array, the function continues, starting from the first value(100) to the last(10), then your new variable "currentTemp" will be equal to the current value "i" is located at in the temperature array, which is then logged to the console.