Dress to Impress


#1

This should print out "shorts" but still getting false?

var suitcase = {
shirt: "Hawaiian",
shorts: "red",
};

if (suitcase.hasOwnProperty ("suitcase.shorts")) {

console.log ("shorts");

}

else
{
console.log ("false");
}


#2

Hi! I guess you should just put 'shorts' within parenthesis instead of 'suitcase.shorts'!


#3

is dot notation not reliable?


#4

Dot notation is Just wrong in this case because when you call hasOwnProperty you call It on an object (in this case suitcase). You are checking if suitcase has a property called 'shorts'! So within hasOwnProperty parenthesis you write the name of the property you are look in for.If you write
suitcase.hasOwnProperty( ' suitcase.shorts') It's gonna check if the object ha a property named suitcase.shorts, not just shorts. This is just the correct syntax of hasOwnProperty.


#5

"hasOwnProperty" returns "true" if condition is right. Since you added ==> shorts:"red" in var suitcase { }, it should print "red" because this condition will be true. You just need a little modification.


if(suitcase.hasOwnProperty("shorts")){
console.log(suitcase.shorts);
}else{
//to print "blue", just remove shorts:"red" from var suitcase
suitcase.shorts = "blue"; // assign any value you want.
console.log(suitcase.shorts);
}