'Objects and comparing in Javascript, how to pass the value into the method
I got this Javascript exercise. I couldn't figure out how to pass the value of the pay of each person into the "comparePay" method. I'd be glad if you help me this.
Thank you in advance.
Create 3 Person objects, with the attributes "name", "occupation" and "pay", as well as the method "comparePay(person)", which compares the pay values of the object calling it and the object passed to it as argument, then printing the result:
[name of object calling the method] earns [difference] more than [other's name]
[name of object calling the method] earns [difference] less than [other's name]
[name of object calling the method] earns as much as [name]
The objects should have the following values:
Michael, JS-programmer, 5000
Lena, Python-programmer, 1500
Brad, Teacher, 800
The output should be like this.↓
First person's name: Michael
Second person's job: Python-programmer
Third person's pay: 800
Michael earns 3500 more than Lena
Brad earns 700 less than Lena
Brad earns as much as Brad
Here is what I have done so far.
function Person(name, occupation, pay) {
this.name = name;
this.occupation = occupation;
this.pay = pay;
this.comparePay = function(person) { /* compare the value of the pay */
var difference = person.pay - person.pay;
if (difference > 0) {
return person.name + " earns " + difference + " more than " + person.name;
} else if (difference === 0) {
return person.name + " earns as much as " + person.name;
} else {
return person.name + " earns " + Math.abs(difference) + " less than " + person.name;
}
};
}
person1 = new Person("Michael", "JS-programmer", 5000);
person2 = new Person("Lena", "Python-programmer", 1500);
person3 = new Person("Brad", "Teacher", 800);
and here is the prepared code, so they are not allowed to be edited.
console.log("First person's name: " + person1.name);
console.log("Second person's job: " + person2.occupation);
console.log("Third person's pay: " + person3.pay + "\n");
person1.comparePay(person2);
person3.comparePay(person2);
person3.comparePay(person3);
Solution 1:[1]
Inside the this.comparePay
function you should use this
to access the object properties:
this.pay
, this.name
, etc.
this.comparePay = function(person) {
var difference = this.pay - person.pay;
...
}
function Person(name, occupation, pay) {
this.name = name;
this.occupation = occupation;
this.pay = pay;
?this.comparePay = function(person) { /* compare the value of the pay */
var difference = this.pay - person.pay;
if (difference > 0) {
return this.name + " earns " + difference + " more than " + person.name;
}
if (difference === 0) {
return this.name + " earns as much as " + person.name;
}
return this.name + " earns " + Math.abs(difference) + " less than " + person.name;
};
?}
person1 = new Person("Michael", "JS-programmer", 5000);
person2 = new Person("Lena", "Python-programmer", 1500);
person3 = new Person("Brad", "Teacher", 800);
console.log(person1.name);
console.log(person1.pay);
console.log(person2.name);
console.log(person2.pay);
console.log(person3.name);
console.log(person3.pay);
console.log( person1.comparePay(person2) );
console.log( person3.comparePay(person2) );
console.log( person2.comparePay(person1) );
console.log( person3.comparePay(person3) );
Tips: You can also remove the else/if
statements since you are returning inside the if
statements. Paulic-crtn
's advice is also important, so try to stick to let/const
when declaring variables in JS.
Solution 2:[2]
I think the problem comes from your comparaison (you're comparing the same object attributes). You want to compare the object passed in the function's argument with the current object.
So I guess you can try to replace
var difference = person.pay - person.pay;
with:
const difference = this.pay - person.pay;
Same for
return person.name + " earns " + difference + " more than " + person.name;
should be
return this.name + " earns " + difference + " more than " + person.name;
Otherwise it will return the same names. You can also use template literals notation
return `${this.name} earns ${difference} more than ${person.name}`;
Hope it makes sense. Also don't forget to use let
or const
instead of var
:)
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 | |
Solution 2 | Kostas Minaidis |