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.

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!

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!

Writing the Function:

Time to take it step by step!

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

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

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

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

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

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!

Software Engineer in the making.