'JavaScript Object planned output does't match
End of the code is:
console.log ("Name: " + pete.fullname() + "\tAge:" + pete.age);
console.log ("Name: " + cara.fullname() + "\tAge:" + cara.age);
console.log (cara.firstname + "is" + (cara.age - pete.age) + " year older than " + pete.firstname);
I wrote it:
pete = {
firstname: "Pete",
lastname: "Programmer",
age: 20
};
var cara = {
firstname: "Cara",
lastname: "Coder",
age: 30,
};
pete.fullname = function (){
console.log (pete.firstname + pete.lastname)
};
cara.fullname = function () {
console.log (cara.firstname +
cara.lastname)
};
console.log ("Name: " + pete.fullname() + "\tAge:" + pete.age);
console.log ("Name: " + cara.fullname() + "\tAge:" + cara.age);
console.log (cara.firstname + "is" + (cara.age - pete.age) + " year older than " + pete.firstname);
Planned output:
Name: Pete Programmer Age:20 Name:
Cara Coder Age:32
Cara is 12 years older than Pete
Don't understand why I have this output:
PeteProgrammer
Name: undefined Age:20
CaraCoder
Name: undefined Age:30
Cara is 10 years older than Pete
Solution 1:[1]
In pete.fullname() and cara.fullname() functions you're doing console.log() instead of return. The code:
pete = {
firstname: "Pete",
lastname: "Programmer",
age: 20
};
var cara = {
firstname: "Cara",
lastname: "Coder",
age: 30,
};
pete.fullname = function (){
return pete.firstname + pete.lastname
}
cara.fullname = function () {
return cara.firstname +
cara.lastname
};
console.log ("Name: " + pete.fullname() + "\tAge:" + pete.age);
console.log ("Name: " + cara.fullname() + "\tAge:" + cara.age);
console.log (cara.firstname + " is " + (cara.age - pete.age) + " year older than " + pete.firstname);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kuriix |
