Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Multiplying values in an object in JavaScript

I am creating an object in JavaScript that holds employee information (name, rate, hours, gross pay). In order to calculate the gross, I need to multiply the hours times the rate. My teacher says I should be able to do it right inside my object, but it is not working. Here is my code:

const emp1 = {
  name: "John Doe",
  rate: 13.25,
  hours: 20.25,
  gross: rate * hours
};
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In javascript you can define functions (methods) on an object and call them as you need :)

Note the method syntax, and the use of 'this' to refer to properties on the object itself.

var emp1 = {
        name: "John Doe",
        rate: 13.25,
        hours: 20.25,
        gross: function () {
          return this.rate*this.hours
        }
    };

console.log(emp1.gross())

about 4 years ago · Juan Pablo Isaza Report

0

Another way was to use the get syntax ...

const emp1 = {
  name: "John Doe",
  rate: 13.25,
  hours: 20.25,
  get gross () { return this.rate * this.hours; },
};
console.log('emp1.gross ...', emp1.gross);

Thus the idea of cause is the same like what James McGlone did suggest, one just is free of omitting the call operator when accessing the value of this (computed) property.

Both solutions are equally valid, one just does not know which one will please the teacher.

about 4 years ago · Juan Pablo Isaza Report

0

You need to declare the variables outside the object first

var rates = 13.25
var hours = 20.25

var emp1 = {
    name : "John Doe",
    rates,
    hours,
    gross: rates * hours
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!