Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Objects and comparing

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

My code Including Grey code
My output using the code below (Expected output on the right)

 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);

>Solution :

What @Major Productions mentioned in his comment is correct. You need to reference current object as this.

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) {
      console.log(this.name + " earns " + difference + " more than " + person.name);
    } else if (difference === 0) {
      console.log(this.name + " earns as much as " + person.name);
    } else {
      console.log(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("First person's name: " + person1.name);
console.log("Second person's occupation: " + person2.occupation);
console.log("Third person's pay: " + person3.pay);

person1.comparePay(person2);
person3.comparePay(person2);
person3.comparePay(person3);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading