8) This shouldn't work and yet it does. Can anyone tell me why?


#1

var suitcase = {
shirt: "Hawaiian"

};

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

    }

#2

Hi, you didn't defined if statement correctly, here is my code which works for you to check what is wrong.

var suitcase = {
    shirt: "Hawaiian"
};

if(suitcase.hasOwnProperty('shirt') === true){
    console.log(suitcase.shirt);
} else {
    suitcase.shirt = "Bungaloo";
}

#3

Aside, fyi,

suitcase.hasOwnProperty('shirt')

is a Boolean expression yielding only a true or a false.

if (boolean_expression) { // yields true } else  { // default }

also yields only a Boolean. Therefore,

if (true) { console.log("It's true") }    // It's true

Which brings us to the conclusion, then, that we can write,

if (suitcase.hasOwnProperty('shirt')) {
    console.log(suitcase.shirt);     // Hawaiian
} else {
    suitcase.shirt = "Bungaloo";
    console.log(suitcase.shirt);     // Bungaloo
}

#4

Here is the answer that worked for me:

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


#5

This works to me:

var suitcase = {
shirt: "Hawaiian"
};
if (suitcase.hasOwnProperty('shorts')) {
console.log('shorts')
} else {
suitcase.shorts = "Hakim";
console.log('shorts')
};


#6

works but it says print the value


#7

Here's mine:

var suitcase = {
    shirt: "Hawaiian"
};

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

#8

This is exactly what I have. I guess I went with this train of logic because obj.hasOwnProperty already returns a Boolean (True/False) value; therefore its not necessary to include it in your if statement. So, if its true then it would console.log the value of the property shorts in the editor; but, its false. So, we result to the else which creates a new value for the property shorts in the suitcase object. Then finally we console.log that value in the editor.