So like the title says, if we are close to the end of the month, we can check if a given weekday (or array of weekdays) will fall from the current day, to the end of the month. If any of the given days appear, return true, else return false.
I'm quite inexperienced with JS, and this is my first time using the map function. I think splitting the two functions is ok, although I've seen map used with a nested anonymous function.
Any feedback on the solution or the use of map would be appreciated.
// Remember, in the JS date object, months are zero based, days are one based XD
// take a project weekday, and check if it falls on the remaining days of the current month
function check_days_remaining(day) {
var d = new Date();
var days_in_month = new Date(d.getYear(), (d.getMonth() + 1), 0).getDate();
var month_day = d.getDate();
var month_days_remaining = days_in_month - month_day;
for(i = 1; i <= month_days_remaining; i++) {
var index_date = new Date(d.getYear(), (d.getMonth() + 1), (d.getDate()+i) );
var index_day = index_date.getDay();
if(index_day == day) {
//console.log("Weekday: " + index_day + " falls on " + index_date);
return true;
}
}
}
function days_remaining(days) {
remaining = days.map(check_days_remaining);
// console.log(remaining); // logs an array of true or undefined elements
return ( remaining.indexOf( true ) > -1 ) ? true : false; // true if at least one project day is remaining in current month
}
var stay_on_month = days_remaining([5,6]); // set whether to stay on current month or display next month
console.log(stay_on_month); // check result
undefined
values in the array.undefined
always makes me nervous for some reason. I've managed to add areturn false
line after the loop in case it exits the loop without returning. \$\endgroup\$