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

whats wrong with my code?

why do i keep getting unidentified?

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

function print (message) {
  console.log(message);
}

function listValues (values) {
  var listHTML = '<ol>';
  for ( var i = 0; i < values.length; i += 1) {
    listHTML += '<li>' + values[i] + '</li>';
} 
listHTML += '</ol>';
print (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

I would say, that the answer, has gone way out from what the question is, the question is simply asking, to log, the values either using for or while loop 2 functions are redundant, for a so small task like this. question does not ask you to print

just have to not think too much on this exercise, your loop is alright, just have to clean the code, and make it simple.

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

function iterate (values) { var valHTML = "<ol>"; for ( var i = 0; i < values.length; i += 1) { valHTML += "<li>"+values[i]+"</li>"; } valHTML += "</ol>"; }

console.log(iterate(temperatures));

I changed it to look like this i get a message saying "The items in the array weren't logged out in order. Your should log all the temperatures in order, starting at 100 and ending at 10." what could be wrong?

Jerry Wu
Jerry Wu
1,845 Points

Hi Anoud,

It looks like the function, which you named iterate, does not actually return anything. Within the function, you have a variable (valHTML) which you are correctly modifying with your for-loop. I believe all you needed was a simple return line (see the comment within the code below):

var temperatures = [100,90,99,80,70,65,30,10];
ā€‹
function iterate (values) {
var valHTML = "<ol>";
for ( var i = 0; i < values.length; i += 1){
    valHTML += "<li>"+values[i]+"</li>";
}
valHTML += "</ol>";
//return valHTML;
}
ā€‹
console.log(iterate(temperatures));

Good luck!