[solved] #8 Dress to impress


#1
var suitcase = {
    shirt: "Hawaiian"
};

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

Why does this work? Is it correct, or is it not correct?
~Thanks


#2

Please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:

The code executes without error. But, I think that it has many logical errors. If this is a exercise related question, I would be able to help better if you give the exercise link.

I don't see a reason why it shouldn't work. Could you please state the statement which is difficult to comprehend for you so I can explain it?


#3

https://www.codecademy.com/en/courses/objects-ii/1/3?curriculum_id=506324b3a7dffd00020bf661#
Is the link, i've seen other people do it differently and i'm wondering if I've done it correctly.


#4

Well it explains in the lesson that ".hasOwnProperty()" in an object means that the variable in an object already has a value:

var object = {
   variable: "value"
};

if (object.hasOwnProperty("variable")){

  //the variable has a value so it will run the code here!
}

I hope this explains your inquiry. If you need me to rephrase this just let me know and I'll see what I can do!


#5

Thanks, but is my code correct for the lesson, it passes but is it correct?


#6

Judging by the aim of the error, your code should not work because the exercise expects you to check if suitcase has the property shorts, not input. Compare:

function test(input) {
    if(suitcase.hasOwnProperty("input")){ // input string; your code

with:

function test(input) {
    if(suitcase.hasOwnProperty(input)){ // input variable; expected code

and see where you gone wrong. Also, instead:

return suitcase.shorts = "red";

you should have:

console.log(suitcase.shorts = "red");

as the exercise expects you to set the shorts property to any value you wish using dot notation. Then print the value of the shorts property.

But the SCT is pretty lenient here so you passed nevertheless.


#7

Thank you my friend.