Search code examples
javascriptdayofweek

Using day of week to return specific object


I am currently working on a fitness application and am aiming for a user of the site to click a button and they will be returned a workout which is recommended on the day of the week it is. For example if its a Monday and the user presses the button it should return a plan for Monday, what JS methods would be suitable for this?

I think I'm going to have to use getDate and if loops but I'm not sure, still relatively new to javascript and exploring the ways to do things so I am open to various ways to accomplish this


Solution

  • const date = new Date // today's date

    const day = date.getDay() // weeks day number starting with 0 for Sunday, 1 for Monday and so on

    then for instance you have an array of workouts workouts = [0, 1, 2, 3, 4, 5, 6]

    starting again with Sunday so you can get an exact one using the index

    array[1] // will return a workout for Monday