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 trialluisgomez7
5,557 PointsMistake 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]); }
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.
<!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
Treehouse TeacherHi 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!
Steven Parker
231,275 PointsIt 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 afor in
loop ,,,
luisgomez7
5,557 PointsThanks 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
Treehouse TeacherThat 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!
Jennifer Nordell
Treehouse TeacherSteven Parker there is already a correction in the Teacher's Notes associated with the video.
luisgomez7
5,557 PointsThanks 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
Treehouse TeacherThen 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"
Steven Parker
231,275 PointsI 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.