Lesson Not Intuitive

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

In the lesson Basic Data Structures: Iterate Through the Keys of an Object with a for…in Statement, I have a suggestion to make a portion of it more intuitive. I got confused upon the first reading and attempting of the challenge.

The issue concerns this snippet of code in particular:

for (let user in users) {
  console.log(user);
};

It’s not intuitive that user is referring to a string, and not to another object. It’s not explained very well in the lesson text.

I think that if it were made explicit that user was referring to a string, it would lead to less confusion. Students would recognize that they couldn’t just access something like user.someProperty, and would have to access the parent object like parentObject.user.someProperty.

“Sometimes you may need to iterate through all the keys within an object.”

An object key is always a string.

1 Like

Ok thanks for the clarification. I don’t remember if this was made explicit in the earlier sections on Objects.

It was in the first lesson on objects, which I know several challenges ago and I’m sure that your brain is crammed full of new material.

In this example, all the properties are stored as strings, such as - "name" , "legs" , and "tails" . However, you can also use numbers as properties. You can even omit the quotes for single-word string properties…

However, if your object has any non-string properties, JavaScript will automatically typecast them as strings.

1 Like