-2

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

  • where is the code? – brk Jun 29 '20 at 14:02
  • getDate gives you the day of the month; for the day of the week, you want `getDay`. And that gives you a value between 0 and 6 - which you can easily use to access the data for the corresponding weekday in an _array_. – CBroe Jun 29 '20 at 14:03

1 Answers1

0

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

Konstantin
  • 26
  • 3