Accessing Object Properties III


#1

Hi,

I’m wondering if you can help me understand this please.

The solution is:

let person = {
name: ‘John’,
age: 53,
weekendAlarm: ‘No alarms needed’,
weekAlarm: ‘Alarm set to 7AM’
};

let day = ‘Wednesday’

let alarm;

if (day === ‘Saturday’ || day === ‘Sunday’) {
alarm = ‘weekendAlarm’;
} else {
alarm = ‘weekAlarm’;
}

But in my mind it should be:

let person = {
name: ‘John’,
age: 53,
weekendAlarm: ‘No alarms needed’,
weekAlarm: ‘Alarm set to 7AM’
};

let day = ‘Wednesday’

let alarm;

if (day === ‘Saturday’ || day === ‘Sunday’) {
alarm = person.weekendAlarm;
} else {
alarm = person.weekAlarm;
}

Why do we not have to reference the object before the key?

Thanks,


#2

The solution as shown above is wrong. It assumes the variables are global, rather than attributes of the object.

Please post a link to this exercise, and make a point of it (including a link) in future topic openers.


#3

So my interpretation was correct?

I believe the link is:

https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/object-property-access-iii?action=resume_content_item&course_redirect=introduction-to-javascript


#4

Actually, no. On examining the lesson there is some extra code (in my workspace).

console.log(person[alarm]);

In that context, the given solution is correct. Note that alarm holds a key name, which is applied in the statement above.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.