Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

Which for loop method is best?

Hi guys, im revisiting the JaavaScript Loops, Arrays and Objects course.

I came across this challenge and struggled a little.

I used the following loop. This works in the console but did not pass the challenge. I used const instead of var.

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

In order to pass the challenge i had to use,

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

Since both work (in the console at least) which method would be best to log out temperature?

2 Answers

Steven Parker
Steven Parker
222,048 Points

Using "let" is a more modern choice and would generally be considered "best practice". The evaluation engine for this challenge is probably older than ES6 and did not recognize the newer keyword.