Intro to Objects II: Dressed to Impress


#1

https://www.codecademy.com/courses/objects-ii/1/3?curriculum_id=506324b3a7dffd00020bf661#

I'm having a bit of trouble with this code. I keep getting a "ReferenceError: shorts is not defined" message every time I try to run this code. To my understanding, the only reason I would receive this message would be if my 'if(suitcase.hasOwnProperty('shorts'))' code were running as true even though it is false, yet I'm still receiving the error. If anyone could shed a light on this for me that'd be awesome.


var suitcase = {
    shirt: "Hawaiian"
};

if(suitcase.hasOwnProperty('shorts')) {
    console.log(shorts);
} else {
    suitcase.shorts = "red";
    console.log(shorts);
};


#2

short is part of the suitcase object? you can use suitcase.shorts


#3

I used 'suitcase.shorts' in the else statement to assign the value "red" to shorts. I'm still receiving the error.


#4

can i see an updated version of your code?


#5

There isn't an updated version, in my original code I under the else statement you can see it.


#6

here you did it right:

suitcase.shorts = "red";

agree, but i wasn't talking about that line, i was talking about this line:

console.log(shorts);

shorts is a property of suitcase object, so you need objectName.propertyName


#7

Ah I see now, that fixed it. Thank you!

suitcase.shorts = "red";
console.log(suitcase.shorts);


#8