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 Data Using Objects Accessing All of the Properties in an Object

Mistake in Treehouse software regarding "for in" loops

A solution I used for one of the objects runs in my Chrome console but not on the Treehouse software. Below is the question:

Use a for in loop to log each of the property names of the shanghai object to the console.

var shanghai = { population: 14.35e6, longitude: '31.2000 N', latitude: '121.5000 E', country: 'CHN' };

My answer: for (prop in shanghai) { console.log((prop), ': ', shanghai[prop]); }

script.js
var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};

for (prop in shanghai) { 
  console.log((prop), ': ', shanghai[prop]);
}

The Treehouse website returns the following answer:
"It's a good idea to use the `var` keyword in a `for in` loop like this: `for (var key in shanghai)`."

I tried doing that but the code won't run. When I use my coding in my console it runs beautifully.
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great, and you do have a working solution in the browser. However, these challenges are run using strict mode (or many of them are). Strict mode requires that you declare variables before you use them with either a var, let, or const. Code run directly in the console of your browser will not be using strict mode, which can lead to a false positive when testing.

So, if I change this:

for (prop in shanghai)

To this:

for (var prop in shanghai)

Hope this helps! :sparkles:

Steven Parker
Steven Parker
231,248 Points

It works, but it relies on implicit global declaration, which not considered a good practice. And in this challenge, the validator checks for and rejects it specifically.

Bummer: It's a good idea to use the var keyword in a for in loop ,,,

Thanks for your answer. Your correction works nicely but I must point that I used my solution because it's the one demonstrated in the preceding video. Demonstrating one method in an instructional video and then asking for another in an objective is confusing.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

That is very true. It is confusing which is why there is a correction posted in the "Teacher's Notes" under that video. They used var in the slide shown at around 1:27 of the video, and you can find this in the Teacher's Notes:

In the for in loop in this video, we left out var — this is a mistake. The correct code should be:

for (var prop in person) {
 console.log(prop, ': ', person[prop]);
}

But I'm glad you have it working now! :sparkles:

Thanks for pointing that out but I really think the video should be corrected rather than placing the correction in the teacher's notes. That's why I didn't notice it.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Then I would suggest you submit that as a suggestion to the Staff at Treehouse. You can reach them at help@teamtreehouse.com. That being said, it's always a good idea to check the Teacher's Notes. Many times, it's simply links to documentation, but sometimes you can find corrections, jokes, and even "easter eggs" :smiley:

Steven Parker
Steven Parker
231,248 Points

I expect it's not easy to correct a video. But I'd bet anything mentioned in the teacher's notes will be corrected when the course gets refreshed.