skip to Main Content

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.

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

2

Answers


  1. Just do:

       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;
             var comparison = "";
             if (difference > 0) {
                 comparison = person.name + " earns " + difference + " more than " + person.name;
             } else if (difference === 0) {
                 comparison = person.name + " earns as much as " + person.name;
             } else {
                 comparison = person.name + " earns " + Math.abs(difference) + " less than " + person.name;
             } 
             console.log(comparison);
             return comparison;
            };
         }
    
    Login or Signup to reply.
  2. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search