8. Dressed to impress, syntax error


#1

var suitcase = {
shirt: "Hawaiian"
};

if( suitcase.hasOwnProperty('shorts'){
return nothing;
}else{
suitcase.shorts = "blue";
console.log(suitcase["shorts"]);
}

what am I doing wrong?


#2

@blondesquirrel,
With your

return nothing;

you are trying to return the associated-Value of the variable nothing
which you have not defined.....


#3

What should be put under the if?


#4

@blondesquirrel,
The Instructions
If your if statement evaluates to true, print the value of the shorts property. ( with console.log() )


#5

Hey I did it like that and is working, hope it will helps

var suitcase = {
shirt: "Hawaiian"
};

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


#6

var suitcase = {
shirt: "Hawaiian"
};

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


#7

I have this code that doesn't work:

var suitcase = {
    shirt: "Hawaiian"
};

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

I get this error message :

Oops, try again. It looks like you have an error in your code. Here's the message: ReferenceError: suitecase is not defined

does anyone can tell what I'm doing wrong? muchas gracias! :slight_smile:


#8

The first condition is false, so your code is creating a property that's already there. You have to turn it on its head.

var suitcase = {

shirt: "Hawaiian"

};

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

suitcase.short=== "cargo";
console.log(suitcase.shorts);

}
else {
console.log(suitcase.shorts);
}


#9

@florin.baci, you have syntax errors.

You created suitcase NOT suit E case:

...
else {
   suitcase.shorts = "green";
   console.log(suitecase.shorts);
};

#10

@mphokuhle, suitcase doesn't have property shorts:

if (suitcase.hasOwnProperty ('shorts')) { // returns false
  suitcase.short=== "cargo"; // wrong operator: use = instead ===
  console.log(suitcase.shorts);
}
else {
  console.log(suitcase.shorts); // error
}

I thought that if suitcase doesn't have prop short we need create it, if has - just print this prop. Isn't it?


#11

Just in case somebody came up with a different answer just like me :

if(suitcase.hasOwnProperty("shorts")==true ){
console.log(suitcase.shorts);
}
else{
suitcase.shorts = "red";
console.log(suitcase.shorts);
};
This works as well I have set the if statement to true at the very beginning just not to confuse myself.


#12

if(suitcase.hasOwnProperty(“shorts”)==true ){
console.log(suitcase.shorts);
correct: if(suitcase.hasOwnProperty(“shorts”)