One thing about the 'export default' in work around project

the project link is hereWorkAround

from step 1 to step 9, we modify the employee.js and export default Employee, but we did not change payGrade into Employee’s property. Does that mean that this payGrade is not exported?
if this payGrade is not exported, how would workAround.js run successfully? actually it did run successfully.

this is the employee.js 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 (Employee.salary >= payGrades.entryLevel.minSalary && Employee.salary <= payGrades.entryLevel.maxSalary) {
return ‘entryLevel’;
} else if (Employee.salary >= payGrades.midLevel.minSalary && Employee.salary <= payGrades.midLevel.maxSalary) {
return ‘midLevel’;
} else return ‘seniorLevel’;
};

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

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

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

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

export default Employee;

and this is the workAround.js code:
import Employee from ‘./employee’;

function getEmployeeInformation(inputSalary) {
Employee.salary = inputSalary;
console.log('Cadre: ’ + Employee.getCadre());
console.log('Tax: ’ + Employee.calculateTax());
console.log('Benefits: ’ + Employee.getBenefits());
console.log('Bonus: ’ + Employee.calculateBonus());
console.log('Reimbursement Eligibility: ’ + Employee.reimbursementEligibility() + ‘\n’);
};

getEmployeeInformation(10000);
getEmployeeInformation(50000);
getEmployeeInformation(100000);

and work out good. this is the result:
Cadre: entryLevel
Tax: 500
Benefits: health
Bonus: 200
Reimbursement Eligibility: 5000

Cadre: midLevel
Tax: 5000
Benefits: health, housing
Bonus: 1000
Reimbursement Eligibility: 13000

Cadre: seniorLevel
Tax: 20000
Benefits: health, housing, wellness, gym
Bonus: 2000
Reimbursement Eligibility: 31000

I think the payGrades object is not exported, how does it work? If the payGrade exported successfully, but we did not add any ‘Emlpoyee.’ before payGrade, I am confused.

In the workAround file you are not making any direct reference to the payGrade object, therefore there is no need to import it.

Methods called on Employee do use the payGrade object but the code that covers this relationship is in the employee.js file which is sufficient.

Since we do not have to import payGrade, does that mean that payGrade is useless in the file? Then I deleted payGrade in the employee.js, the result shows error as followings:
/home/ccuser/workspace/intermediate-javascript_modules-workAround/employee.js:11
if (Employee.salary >= payGrades.entryLevel.minSalary && Employee.salary <= payGrades.entryLevel.maxSalary) {
^

ReferenceError: payGrades is not defined
at Object.Employee.getCadre (employee.js:8:26)
at getEmployeeInformation (workAround.js:5:36)
at Object. (workAround.js:12:1)
at Module._compile (module.js:571:32)
at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Function.Module.runMain (module.js:605:10)

payGrade is required in the employee.js file which is why you are getting errors on deleting it.

It is not required in the workAround.js file as jt does not do anything directly there.

1 Like

so only export the directly related ones? am I right?
Thank you very much.

Yes, your initial working code was correct. You only import the elements that you need to call or refernce directly in the receiving file.

The imported elements will continue to interact with the other elements in their originating file as they require.