4

How can I get all weekday names between 2 weekdays as parameters? It should also return accurately when it get past the 7 days.

My week format is:

'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'

Example and expected output below. Thanks

function day(first, last) {
  var day = new Date();
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

  for (i = 0; i < 14; i++) {
    console.log(week[(day.getDay() + 1 + i) % 7]);
  }

}

day('Tuesday', 'Thursday'); // output should be "Tuesday, Wednesday, Thursday"
day('Friday', 'Tuesday'); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
day('Saturday', 'Monday'); // output should be "Saturday, Sunday, Monday"
random_user_name
  • 25,694
  • 7
  • 76
  • 115
marknt15
  • 5,047
  • 14
  • 59
  • 67
  • So in your case you presume the function only work for same year same month(no crossing of months occur) and only return this in a pre-set way? – gitguddoge Sep 20 '18 at 02:20
  • 1
    @gitguddoge The function gets passed two days of the week. Dates themselves don't play a part. – Tyler Roper Sep 20 '18 at 02:21

9 Answers9

3

You could manipulate the array to avoid using loops. Code is commented for clarity.

function day(first, last) {
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

  var firstIndex = week.indexOf(first);          //Find first day
  week = week.concat(week.splice(0,firstIndex)); //Shift array so that first day is index 0
  var lastIndex = week.indexOf(last);            //Find last day
  return week.slice(0,lastIndex+1);              //Cut from first day to last day
}

console.log(day('Tuesday', 'Thursday'));
console.log(day('Friday', 'Tuesday'));
console.log(day('Saturday', 'Monday'));
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
2

Something like this:

function day(first,last) {
  var week=new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var i=week.indexOf(first), result=[];
  do {
    result.push(week[i]);
    i=(i+1) % week.length;
  } while (week[i]!==last);
  result.push(last);
  return result;
}
Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
2

I think I would just return two different cases depending on whether the range extended past the weekend. This will just return the slice if start is earlier in the week. Otherwise it returns the two parts piecewise:

function day(first, last) {
    var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    let start = week.indexOf(first)
    let end = week.indexOf(last)
    return (start > end)
        ? [...week.slice(start), ...week.slice(0, end+1)]
        : week.slice(start, end+1)
  
  }

  console.log(day('Tuesday', 'Thursday'))
  console.log(day('Friday', 'Tuesday'))
  console.log(day('Saturday', 'Monday')) 
Mark
  • 90,562
  • 7
  • 108
  • 148
0

Here is one way you might do it:

function day(first, last) {
  var day = new Date();
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  //Double the array to account for going from end of the week to the beginning
  var weeks = week.concat(week);
  var dayArray = [];
  var activeDay = false;
  //Loop through the large week array. 
  for (var x=0; x<weeks.length; x++) {
     var day = weeks[x];
    //Start adding to the array on the first day
    if (day == first) {
        activeDay = true;
    }
    //Start adding to the array on the first day
    if (activeDay) {
        dayArray.push(day);
      //If the last day then exit
      if (day == last) {
        return dayArray;
      }
    }

  }
    //Return an empty array if no matches
    return [];
}

day('Tuesday', 'Thursday'); // output should be "Tuesday, Wednesday, Thursday"
day('Friday', 'Tuesday'); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
day('Saturday', 'Monday'); // output should be "Saturday, Sunday, Monday"
pg316
  • 1,380
  • 1
  • 8
  • 7
0
function day(first, last) {
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

  for (i = 0; i < 7; i++) {
     if(week[i]==first){
        for(j=i ; i< 14 ;j++){ 
            console.log(week[j%7]);
            if(week[j]==end){
               return;
            }
        }

     }
  }

}

day('Tuesday', 'Thursday'); // output should be "Tuesday, Wednesday, Thursday"
day('Friday', 'Tuesday'); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
day('Saturday', 'Monday'); // output should be "Saturday, Sunday, Monday"
Mark
  • 90,562
  • 7
  • 108
  • 148
0

Try this way

function GetDays(first, last) {
  var day = new Date();
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var start_index=week.indexOf(first);
  var last_index=week.indexOf(last);
  if(start_index<last_index){
     return week.slice(start_index,last_index+1);
  }
  else{
     return [...week.slice(start_index),...week.slice(0,last_index+1)]
  }
}
console.log(GetDays("Sunday","Tuesday"))
console.log(GetDays("Sunday","Sunday"))
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27
0

function day(first, last) {
    var day = new Date();
    var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
 var firstIdx = week.indexOf(first), lastIdx = week.indexOf(last);

 var result = [];
 if(lastIdx >= firstIdx) {
  for (var i = firstIdx; i <= lastIdx && i < week.length; i++)
   result.push(week[i]);
 }
 else {
  for (var i = firstIdx; i < week.length; i++)
   result.push(week[i]);
  for (var i = 0; i <= lastIdx && i < week.length; i++)
   result.push(week[i]);
 }
 return result;
}

alert(day('Tuesday', 'Thursday')); // output should be "Tuesday, Wednesday, Thursday"
alert(day('Friday', 'Tuesday')); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
alert(day('Saturday', 'Monday')); // output should be "Saturday, Sunday, Monday"
Miller Cy Chan
  • 897
  • 9
  • 19
0

I think you should set an index to every weekday.

function day(first, last) {
    var firstIndex;
    var lastIndex;
    var weekDays = [
        { index: 0, name: 'Monday' },
        { index: 1, name: 'Tuesday' },
        { index: 2, name: 'Wednesday' },
        { index: 3, name: 'Thursday' },
        { index: 4, name: 'Friday' },
        { index: 5, name: 'Saturday' },
        { index: 6, name: 'Sunday' }
    ];

    weekDays.forEach(function (item) {
        firstIndex = item.name.toLowerCase() === first.toLowerCase() ? item.index : firstIndex;
        lastIndex = item.name.toLowerCase() === last.toLowerCase() ? item.index : lastIndex;
    });

    if (firstIndex === undefined || lastIndex === undefined) { return; }

    var days = [];
    weekDays.forEach(function (item) {
        if (item.index >= firstIndex && item.index <= lastIndex) {
            days.push(item.name);
        }
    });

    console.log(days.join(', '));
}
sabandurna
  • 86
  • 9
0

Simple, but obvious and effective:

function day(first, last) {
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var found = false; 
  for(var i=0; i<week.length; i++) {
    if (!found) {
        if (week[i] == first) found = true;
    }
    if (found) {
        console.log(week[i]);
    }
    if (found && week[i] == last) {
        return;
    }
  }
}

Try it online!

Turophile
  • 3,367
  • 1
  • 13
  • 21