Accessing Object Properties III

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,

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.

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

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.

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