I have hit a brick wall with a JS exercise :/. I am trying to 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.
Expected output.:
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
The objects should have the following values:
Michael, JS-programmer, 5000
Lena, Python-programmer, 1500
Brad, Teacher, 800
I have created a code that prints the first part of the exercise but not able to print the comparisons. Please see the screenshot for clarification. The grey code below for comparisons are not editable so I wasn’t sure how I could print the comparisons as they dont contain the console.log bit. Should I do this by changing the compare Pay() function.
The expected output printing order should be followed to pass, so first the names and details and second the comparisons.
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);
2
Answers
Just do:
What @Major Productions mentioned in his comment is correct. You need to reference current object as this.