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
  • \$\begingroup\$ Your check days remaining function never returns false \$\endgroup\$ Commented Sep 28, 2016 at 14:50
  • \$\begingroup\$ Is it bad that it doesn't return false? \$\endgroup\$ Commented Sep 28, 2016 at 15:02
  • \$\begingroup\$ Ive never dealt with the question "what happens if a return function returns nothing" because i always write it that it returns something. Readable is better than short... \$\endgroup\$ Commented Sep 28, 2016 at 15:09
  • \$\begingroup\$ Fair enough. It does sound a bit sketchy to see undefined values in the array. undefined always makes me nervous for some reason. I've managed to add a return false line after the loop in case it exits the loop without returning. \$\endgroup\$ Commented Sep 28, 2016 at 20:52

1 Answer 1


Calculate the accepted weekdays range for today's week once, then check the input array for any value in the range.

function weekdaysBelongToTheRestOfThisMonth(weekdays) {
    var today = new Date();
    var todayDay = today.getDate();
    var todayWeekday = today.getDay();

    var maxMonthDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);
    var maxMonthDay = maxMonthDate.getDate();
    var maxMonthWeekday = maxMonthDate.getDay();
    var maxWeekday = maxMonthDay - todayDay > 7 - todayWeekday ? 7 : maxMonthWeekday;

    return weekdays.some(function(d) { return d >= todayWeekday && d <= maxWeekday });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.