0

Please help! i just started learning objects ecc.. i didn't want to go ahead, because i wanted to really understand the Objects on javascript. So i tried to write some codes of an objects with hourly pay,weekly hours,expenses,overtime and 3 functions.

var monthlyPay = {
//hours per week
     hourWeek: 40,
//pay per hours
     hourlyPay: 10,
//all the monthly expenses
     mothlyExpenses: 120,
//additional hours of work
     overtime: 7,

//function that calculates the bonus based on the overtime and hourlyPay plus 4
     calculateBonus: function(){
     this.Bonus = this.overtime * (this.hourlyPay + 4);
 },
//function that calculates the total monthly pay
     calculatePay: function(){
     this.Pay = this.hourWeek * this.hourlyPay + this.Bonus;
 },
//function that calcutes the remaining money
     calculateMoney: function(){
     this.Money = this.monthlyExpenses - this.Pay;
 },

};
monthlyPay.calculateMoney();
monthlyPay.calculateBonus();
monthlyPay.calculatePay();
console.log(monthlyPay);

It runs with no error, but when i look at the browser console the Money property has a value of NaN. Thanks in advance for answering!!

enter image description here

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
CLINToris2
  • 11
  • 2

2 Answers2

0

There are two bugs in your code.
The first one is mentioned by Barden.
Pay is called before its declaration which is undefeined.

Also you mis-spell monthlyExpenses for your variable declaration.
Therefore: a calculation of two undefined value is also undefined.

Try to debug with printing values which may be used for Money:

calculateMoney: function(){
    console.log(`Params : \nmonthlyExpenses: ${this.monthlyExpenses} \nPay: ${this.Pay}`);
    this.Money = this.mothlyExpenses - this.Pay;
}
Result is :
Params : 
monthlyExpenses: undefined
Pay: undefined

To summarise:
1. change the function call orders
2. make monthlyExpenses or mothlyExpenses spell the same

:)

Sam Ho
  • 206
  • 1
  • 4
-1

summarizing what was said:

The order should be

monthlyPay.calculateBonus();
monthlyPay.calculatePay();
monthlyPay.calculateMoney();
console.log(monthlyPay);

And you should write

monthlyExpenses: 120,
Barden
  • 1,020
  • 1
  • 10
  • 17