Skip to content

Instantly share code, notes, and snippets.

@iamshadmirza
Created December 3, 2019 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamshadmirza/ac3a775233bbb90d86a0021849229b01 to your computer and use it in GitHub Desktop.
Save iamshadmirza/ac3a775233bbb90d86a0021849229b01 to your computer and use it in GitHub Desktop.
Solution of weekly problem from Cassidy Williams's Newsletter
// Interview question of the week by Cassidy Williams's Newsletter
// This week's question courtesy of Louis:
// Write a function that takes the name of a day (e.g "Monday", "Tuesday")
// and an integer offset, and returns the name of the day which would be the original + offset.
// Extra credit: do it in 1 line!
// Example:
// > dayOffset("Wednesday", 4)
// > "Sunday"
dayOffset(day, offset) {
const week = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'sunday'];
return week[(week.findIndex(weekday => weekday === day.toLowerCase()) + offset) % 7];
}
console.log(dayOffset("Wednesday", 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment