I'm currently developing an angularjs attendance tracking system and I'm facing challenges when it comes to accurately counting the number of days an employee is absent. Despite attempting to solve this issue with two nested for loops, I am still encountering inconsistencies in the count of absent values.
For instance
The variable days.length encompasses a range of 30 days from 11-1-17 - 11-30-17
getDaysInMonth(10, 2017);
function getDaysInMonth(month, year) {
var date = new Date(year, month, 1);
console.log('month', month, 'date.getMonth()', date.getMonth());
while (date.getMonth() === month) {
days.push(moment(date).format('MM-DD-YYYY'));
date.setDate(date.getDate() + 1);
}
return days;
}
The array objArray contains 4 distinct dates:
11-27-2017, 11-28-2017, 11-29-2017, 11-30-2017
Controller Implementation (using for loop)
for (var y = 0; y < days.length; y++) {
for (var z = 0; z < objArray.length; z++) {
if (days[y] === moment(objArray[z].day).format('MM-DD-YYYY')) {
console.log('entry found', days[y]);
} else {
console.log('absent'); // generating multiple entries
}
}
}
Modification
Adjusted objArray values
objArray: [
{
day: "2017-11-26"
},
{
day: "2017-11-27"
},
{
day: "2017-11-28"
}
]