Intermediate Javascript: Workaround Project, Step 5 Question

Hello,

On step 5 in the Workaround Project, I’m wondering why the properties of the Employee object (getCadre, calculateTax, getBenefits, etc.) aren’t actually inside the Employee object?

The Employee object is empty except for the salary property. How can these other properties be properties of the Employee object when they’re not listed as being inside the object?

Here’s the code:

let Employee = {
  salary: 100000,
};



let payGrades = {
  entryLevel: { taxMultiplier: .05, benefits: ['health'], minSalary: 10000, maxSalary: 49999 },
  midLevel: { taxMultiplier: .1, benefits: ['health', 'housing'], minSalary: 50000, maxSalary: 99999 },
  seniorLevel: { taxMultiplier: .2, benefits: ['health', 'housing', 'wellness', 'gym'], minSalary: 100000, maxSalary: 200000 }
};

Employee.getCadre = function () {
  if (salary >= payGrades.entryLevel.minSalary && salary <= payGrades.entryLevel.maxSalary) {
    return 'entryLevel';
  } else if (salary >= payGrades.midLevel.minSalary && salary <= payGrades.midLevel.maxSalary) {
    return 'midLevel';
  } else return 'seniorLevel';
}

Employee.calculateTax = function() {
  return payGrades[getCadre()].taxMultiplier * salary;
}

Employee.getBenefits = function () {
  return payGrades[getCadre()].benefits.join(', ');
}

Employee.calculateBonus = function () {
  return .02 * salary;
}

Employee.reimbursementEligibility = function () {
  let reimbursementCosts = { health: 5000, housing: 8000, wellness: 6000, gym: 12000 };
  let totalBenefitsValue = 0; 
  let employeeBenefits = payGrades[getCadre()].benefits;
  for (let i = 0; i < employeeBenefits.length; i++) {
    totalBenefitsValue += reimbursementCosts[employeeBenefits[i]];
  }
  return totalBenefitsValue;
}

Because JavaScript offers dozen of different ways to do things, as you can can see here:

Working with objects - JavaScript | MDN

Methods can be added directly when we declare/define the object, but its also allowed to first define a function, and then associate this function as a method on a object.

1 Like