JavaScript for Developers 39 - Code Exercise

preview_player
Показать описание

Here's a small code exercise for you to test your knowledge. Try this out, and check out the solution in the next video.
Рекомендации по теме
Комментарии
Автор

I tried using 'anonymous function' and it worked, but when I try using the arrow function it cannot get the value of the property set in the object
1) this worked : //able to access 'state' from the 'address' obj inside the 'person' object}
2) this didn't : "isFromState" : (inputState) => {//returns value as undefined for 'state'}

why so?

mayurbhawsar
Автор

Thank you very much ! interesting task!

TheGuroguro
Автор

Here we go...

var person = {
"firstName" : "Abhijit",
"lastName" : "Mondal",
"getFullName" : function(){
return this.firstName + " " + this.lastName;
},
"address" : {
"street" : "5th Barron Street",
"city" : "Awesome City",
"state" : "AB"
},
"isFromState" : function(givenState){
if(this.address.state === givenState)
return true;
else
return false;
}
};



The above-mentioned piece of code returns "false".

abhijitmondal
Автор

My exercise answer:

var person = {
"firstName": "Sabirah",
"lastName" : "J",
"getFullName" : function() {
return this.firstName + " " + this.lastName;
},
"address" : {
"street" : "123 JS Street",
"city" : "JS",
"state" : "CA"
},
"isFromState" : function(statePassed) {
return this.address.state === statePassed;
}
};


person.isFromState("ABC");
false
person.isFromState("CA");
true

monsterhuntergo
Автор

var person={

firstName:"amrendra",
lastName:"kumar",

address:{
street: "xyz road",
city:"Dwarka",
state:"delhi"
},

isFromState: function(getCity){


return true;
else
return false;

}

};


AmrendraKumar-koyf
Автор

var person = {
firstName: "safvan",
lastName: "p",
getFullName: function () {
return this.firstName + " " + this.lastName;
},
"address":{
street:"123 JS Street",
city : "JS",
state :"california"
},

// method to check if the state is from user given state
isFromState : function(state){
return this.address.state == state; // true if both states are equal
}
};


safvanp
visit shbcf.ru