Coding for Scrubs: Return The Updated Day (JS)

WELLLLLLCOME BACK TO A NEW SERIES OF CODING FOR SCRUBS. Today we are going to be talking about a problem I recently got from a code assessment.

Days in the week are represented as strings: 
("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")

Pseudocode:

By reading the problem, we know the days that we are working with. In order to access the days, we should put them in an array. If you checked out the previous article on Coding for Scrubs: Anagram Palindrome, we talked about the built-in function indexOf(). We are going to use indexOf() to figure out the index of the day that’s being passed in as the argument.

We now know each day is assigned to a specific number. “Mon” will always have the index number of 0. “Tue” will always have the index number of 1. I know ya’ll get it, but here’s for ya’ll visual learners!

          ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
index # 0 1 2 3 4 5 6

Since we know this information is going to remain constant throughout the problem, whatever day that is being passed in as an argument would know what index number it has. Now, we have to figure out a method to return the new day if the index number exceeds 7 or more.

For example, what if the day that is being passed in as an argument is “Fri” and the number being passed in is 5? We know that five days from “Fri” would be “Wed”. The index of “Fri” is 4 and if we were to add 5 it will equal to 9. What would be the best way to convert 9 and have it equate to “Wed”? MODULE!

1. Make an array of the days 
2. Grab the index number of each day
3. Grab the index of the new day by adding num
4. Module the index by 7
5. Return!

Writing the Function:

Time to take it step by step!

Let’s establish our days by putting them into an array.

function whatDay(day, num){
let days = ["Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"]
}

Now, we have to find the index of the day that is being passed in as an argument.

function whatDay(day, num){
let days = ["Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"]
let dayIndex = days.indexOf(day)
}

We then need to figure out what the new day index would be by adding the dayIndex with the num being passed in.

function whatDay(day, num){
let days = ["Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"]
let dayIndex = days.indexOf(day)
let newDayIndex = dayIndex + num
}

We have the newDayIndex and we now have to module by 7.

function whatDay(day, num){
let days = ["Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"]
let dayIndex = days.indexOf(day)
let newDayIndex = dayIndex + num
let newDayIndex = newDayIndex % 7
}

and now we use the index number and match it up with our days array!

function whatDay(day, num){
let days = ["Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"]
let dayIndex = days.indexOf(day)
let newDayIndex = dayIndex + num
let newDayIndex = newDayIndex % 7
return days[newDayIndex]
}

Conclusion:

Thank you guys for reading another series of Coding for Scrubs.
Here is our finished product! I hope you guys learned a lot. Stay tuned for next week’s article!

function whatDay(day, num){
let days = ["Mon", "Tues", "Weds", "Thurs", "Fri", "Sat", "Sun"]
let dayIndex = days.indexOf(day)
let newDayIndex = dayIndex + num % 7
return days[newDayIndex]
}

Software Engineer in the making.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store