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

Carlos Allembert
Carlos Allembert
3,368 Points

Problem with "Iterating through an array"

What's wrong with the code? It should display the array values in the console but nothing happens, and no error appears in the console. Thanks for your help.

script.js
var temperatures = [100,90,99,80,70,65,30,10];

for (i = 0; i < temperatures.lenght; i ++) {
  var temp = temperatures[i];
  console.log(temp);
}
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

Matthew Lang
Matthew Lang
13,483 Points

Hey!

When using a for loop, make sure to declare the variable 'i' (or whatever you may want to use). For example:

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

// Adding 'var i = 0;' instead of 'i = 0;' with no var
// You also had '.length' spelled wrong, and it was '.lenght'
for (var i = 0; i < temperatures.length; i ++) {
  var temp = temperatures[i];
  console.log(temp);
}

Just a simple typo of '.length', and remember to declare the iterator variable with 'var'.

I have tested this, and it should now work! :)

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Carlos,

Please see the below code I used to pass this challenge:

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

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

Happy Coding!!!