Dress To Impress


#1

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

I got my code to evaluate to true, and I was ok'd to proceed to the next lesson. But is my code correct? I ask b/c I looked at other questions about the same lesson, and my code looks different.


var suitcase = {
    shirt: "Hawaiian"
};

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


#2

Correct in what sense of the word?
In terms of updating json to now have shorts and set it to red? Yes.


#3

Ambiguous use of NOT.

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

The SCT may not be checking for very much, hence you were allowed to pass. The above I think follows more closely to the instructions.


#4

This is not a JSON object, per se, just a JavaScript object.


#5

my wording may have been bad, I mean as a general js object.
Will update post for clarity.


#6

What is SCT?
Also, where did you get the (suitcase.shorts) part. The instructions were to get rid of this console.log statement.


#7

Submission Correctness Test, a script written by the author to check the lesson against a small set of expectations. Not necessarily exhaustive, and not directly tied to the interpreter.

We begin the lesson with,

var suitcase = {
    shirt: "Hawaiian"
};

console.log(suitcase.shorts);

It's the above console.log() statement we are to remove before proceeding with the exercise.

Remove the console.log statement.

The remaining instructions ask us to,

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.


#8

In the If statment....if it evaluates to true, it is to 'print the value of the shorts property.' But what exactly is the shorts property if we haven't defined it? It won't really be defined until the statement evaluates to false. Am I reading too much into this?
I still don't think my code is correct, eh?


#9

It is not defined, exactly. That is why we define it in the else statement. The if statement is expected to fail in this instance.

We are free to add the shorts property directly to the object, for the purpose of testing, but it may not pass. The code I posted above follows the instructions (and also passes).


#12