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

Sana Alkuyam
Sana Alkuyam
2,597 Points

whats wrong with my code?

it worked on google console but not here

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

Antonio De Rose
Antonio De Rose
20,885 Points
/*now there are 2 things, here
1) was that intent not to put the curly braces after the loop
if so, you can provided, there is only one statement after the for loop
correctly so, there is only one statement, which is console.log
so you can ignore the curly, that being the case
there are few errors*/

//example 1 {curly braces optional}
for (initialization; condition; increment)
 one and only statement;


/*
2) if the curly removal after the loop, if it is not intent,
then you have to practice with the curly
*/


//example 2 {curly braces must}
for (initialization; condition; increment){
 one statement;
or many statement;
}

//your case of example
var temperatures = [100,90,99,80,70,65,30,10];
for(var i = 0; i< temperatures.length; i ++); //you cannot have semicolon, after a loop start or an if start
    console.log(temperatures); //here there is an error, you are trying to print the whole array
//when the expectant is a single value

Antonio is right, there are a few problems with your code. I'm rewriting my initial response for my own benefit, but hopefully it will help to supplement the answer that Antonio already gave you(i'm learning too).

The first thing I noticed is that you have a space between the operand(i) and the increment operator(++) in the final-expression of your for loop. This is the main thing that is preventing you from logging the variable temperatures in the console. That being said, you're still going have an error when that gets fixed.

The second problem is the curly braces. I recommend referring to documentation on the Mozilla Developer Network. The MDN is one of the best resources available to you. Even if you don't understand it all right now, you can use it as a reference for what your function is supposed to look like.

When you've fixed those issues, your loop will return the temperatures array to the console, however, I don't think that's what you want. The problem is asking you to return a single value for each index in the array. Like Antonio, I'm not going to tell you how to do this. I recommend searching on MDN or reviewing the lesson to see if you can find the answer for yourself. I will give you a hint though...

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

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