-1

I am following a tutorial on Codecademy and it says

unexpected token {

What is the error here please?

This is what I need to do

Write an if statement that checks to see if suitcase has the shorts property.

If your if statement evaluates to true, print the value of the shorts property.

If your if statement evaluates to false, set the shorts property to any value you wish using dot notation. Then print the value of the shorts property.

var suitcase = {
    shirt: "Hawaiian"
};
if (suitcase.hasOwnProperty("shorts") {
        console.log(suitcase.shorts)
    } else if (suitcase.hasOwnProperty != ("shorts")) {
        suitcase = {
            shorts: "tailormade"
        };
        console.log(suitcase.shorts);
    }
}
6
  • i mean i know its saying unexpected { but i dont see an unexpected { Commented Jul 13, 2013 at 6:46
  • 1
    That's not how function calls work... Commented Jul 13, 2013 at 6:46
  • 1
    if(suitcase.hasOwnProperty("shorts"){. You have not closed the parenthesis. Commented Jul 13, 2013 at 6:46
  • 1
    Check this line if(suitcase.hasOwnProperty!=("shorts") you should remove != Commented Jul 13, 2013 at 6:47
  • @RtrRtr Use something like jslint.com to proof these as they provide a bit more verbatim detail on errors and allows you to debug as fast as you can type until valid. Commented Jul 13, 2013 at 6:58

2 Answers 2

3
var suitcase = {
     shirt: "Hawaiian"
     };

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

      console.log(suitcase.shorts);


     }else if(!suitcase.hasOwnProperty("shorts")){

     suitcase = { shorts: "tailormade"};
      console.log(suitcase.shorts);


    }

Things that changed:

 if(suitcase.hasOwnProperty("shorts"){

Was missing parenthesis

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

And this:

}else if(suitcase.hasOwnProperty!=("shorts")){

Removed != and moved ! in front.

}else if(!suitcase.hasOwnProperty("shorts")){

Do note, that you could just say else, there's no need for an else if. I also removed the last closing bracket and added one semi-colon after the first console.log. And since you're doing console.log(suitcase.shorts); in both places, just move it out of the conditionals all together.

So we could further reduce this code to this:

var suitcase = {
     shirt: "Hawaiian"
     };
 if(!suitcase.hasOwnProperty("shorts")){
     suitcase = { shorts: "tailormade"};
    }
console.log(suitcase.shorts);
Sign up to request clarification or add additional context in comments.

5 Comments

+1, this is the only answer that fix both problems in this code
Hi, thanks so much, how do i do this what is wrong with this code.
var nyc = { fullName: "New York City", mayor: "Michael Bloomberg", population: 8000000, boroughs: 5 }; // write your for-in loop here for( var "population" in nyc){ console.log(population); }
I think he wants to ADD the shorts, not replace the shirt
If you want to add something, say suitcase["shorts"] = "tailormade";
1

Your if statement is missing a ) - whenever you get an "unexpected something" error, always count your parentheses. It helps to use an editor that supports bracket matching, such as Notepad++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.