对象和比较

huangapple go评论75阅读模式
英文:

Objects and comparing

问题

以下是代码的翻译部分:

function Person(name, occupation, pay) {
    this.name = name;
    this.occupation = occupation;
    this.pay = pay;
    this.comparePay = function(person) { /* 比较支付值 */
        var difference = person.pay - this.pay;
        if (difference > 0) {
            return person.name + " 比 " + this.name + " 多赚 " + difference;
        } else if (difference === 0) {
            return person.name + " 和 " + this.name + " 收入一样多";
        } else {
            return person.name + " 比 " + this.name + " 少赚 " + Math.abs(difference);
        }
    };
}

person1 = new Person("Michael", "JS程序员", 5000);
person2 = new Person("Lena", "Python程序员", 1500);
person3 = new Person("Brad", "老师", 800);

希望这个翻译有帮助。

英文:

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

答案1

得分: 3

以下是代码中需要翻译的部分:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->
英文:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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 &gt; 0) {
console.log(this.name + &quot; earns &quot; + difference + &quot; more than &quot; + person.name);
} else if (difference === 0) {
console.log(this.name + &quot; earns as much as &quot; + person.name);
} else {
console.log(this.name + &quot; earns &quot; + Math.abs(difference) + &quot; less than &quot; + person.name);
}
};
}
person1 = new Person(&quot;Michael&quot;, &quot;JS-programmer&quot;, 5000);
person2 = new Person(&quot;Lena&quot;, &quot;Python-programmer&quot;, 1500);
person3 = new Person(&quot;Brad&quot;, &quot;Teacher&quot;, 800);
console.log(&quot;First person&#39;s name: &quot; + person1.name);
console.log(&quot;Second person&#39;s occupation: &quot; + person2.occupation);
console.log(&quot;Third person&#39;s pay: &quot; + person3.pay);
person1.comparePay(person2);
person3.comparePay(person2);
person3.comparePay(person3);

<!-- end snippet -->

答案2

得分: 2

Sure, here's the translated code:

只需执行

function Person(name, occupation, pay) {
    this.name = name;
    this.occupation = occupation;
    this.pay = pay;
    this.comparePay = function(person) { /* 比较薪水的价值 */
        var difference = this.pay - person.pay;
        var comparison = "";
        if (difference > 0) {
            comparison = person.name + "比" + person.name + "多赚" + difference + "。";
        } else if (difference === 0) {
            comparison = person.name + "和" + person.name + "一样多。";
        } else {
            comparison = person.name + "比" + person.name + "少赚" + Math.abs(difference) + "。";
        } 
        console.log(comparison);
        return comparison;
    };
}
英文:

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 = &quot;&quot;;
if (difference &gt; 0) {
comparison = person.name + &quot; earns &quot; + difference + &quot; more than &quot; + person.name;
} else if (difference === 0) {
comparison = person.name + &quot; earns as much as &quot; + person.name;
} else {
comparison = person.name + &quot; earns &quot; + Math.abs(difference) + &quot; less than &quot; + person.name;
} 
console.log(comparison);
return comparison;
};
 }

huangapple
  • 本文由 发表于 2023年4月6日 23:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定