I have this code, used to get a number of hours from an object and multiply them by a variable.
Here is the object 'work'
var work = [
{'day': 27, 'hours': 7.30},
{'day': 29, 'hours': 7.30},
{'day': 31, 'hours': 10},
{'day': 1, 'hours': 8.30},
{'day': 2, 'hours': 7},
{'day': 3, 'hours': 7},
{'day': 5, 'hours': 7.30},
{'day': 6, 'hours': 7},
{'day': 7, 'hours': 7.30},
{'day': 8, 'hours': 8},
{'day': 9, 'hours': 9.30}
]
var payPerHour = 7;
and here my function to calculate the pay
function calculatePay()
{
var result = 0, fResult = 0;
for(var i = 0; i < work.length; i++) {
Object.keys(work).forEach(function (val) {
if (work[i].hasOwnProperty('hours'))
result = work[i][val] * payPerHour;
fResult += result;
});
}
return fResult;
}
I have used "hasOwnProperty" to check if the property "hours" exists in work. The result of the function is NaN. Why?
Object.keys(work): what do you expect here ?.30's make me wonder if your data is off a bit. Should those be.5for half-hours? or:30for 30 minutes, with some further translation still to do before calculation?