JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Iterating through an Array

Conner Williams
PRO
Conner Williams
Pro Student 2,670 Points

how do you complete this exercise?

having trouble getting this one right.

script.js
var temperatures = [100,90,99,80,70,65,30,10];
for(i=100; i < temperatures.length; i-=1) {
  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>

2 Answers

Remember that you should use the index (location) in the array instead of the array value itself. var temperatures = [100,90,99,80,70,65,30,10]; temperatures[0] --> should give you the value of the first item in the array --> 100 in this case temperatures[100] --> doesn't exist since there are only 10 items in the array

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

We defined i = 0 since we want to start from the beginning i++ is the same as : i + 1

This means for each item in the array, add 1 to i which in return console logs the value of i as index of the array.

Paul Scanlon
PRO
Paul Scanlon
Pro Student 24,007 Points

Hey conner try this

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

Happy coding

Paul

Paul Scanlon
Paul Scanlon
Pro Student 24,007 Points

You start at 0, which is the first item in the array (javascript is zero based), not the numerical value of 100, then loop over them using i += 1

Hope i explained it ok