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

gregory gordon
seal-mask
.a{fill-rule:evenodd;}techdegree
gregory gordon
Full Stack JavaScript Techdegree Student 14,652 Points

Use a for or while loop to iterate through the values in the temperatures array from the first item -- 100 -- to the las

not understanding why i can list them in order.

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

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

You both are making way more code then needed.

\\\\\\\\\\\\\\\\\\\\\\\\

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

for (var i= 0; i< temperatures.length; i+= 1) {

console.log(temperatures[i]); }

\\\\\\\\\\\\\\\\\\\\\\

There are a few errors in this code: Both the variable holding the list of temperatures and <ol> generating function have the same name. This makes it so you can't pass the list of temperatures into the function later.

The function declaration isn't closed with a }, so the console.log method is never called as it is still inside the function declaration.

The <ol> generating function never appends a </ol> to listHTML.

The listHTML variable is never returned from the function, so it won't work as it seems you intend with console.log.

In console.log(temperatures), I am uncertain what you are attempting to do here, but I presume you want the output of the function which you named temperatures, but the function isn't called with (temperatures).

In the corrected code below I also change console.log to document.write because this is an awful string to actually read in the log.

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

function logTemperatures( list ) {
  var listHTML = '<ol>';
  for (var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  return listHTML;
}
document.write(logTemperatures(temperatures));