# How can I write this function without a bunch of if else statements?

• A+
Category：Languages

I basically just can't figure this one out and don't want to hack it.

It will look so messy if I do this for 52 weeks in a year.

Any tips?

Update

This question is not about getting the year's current week.

This question is about getting weeks elapsed since a date defined.

I want the next week's workout to show up on 6th day of the current week

Example

My days since start are 99: 2018-05-30 18:39:29.

Some of your examples are showing me on week 15.

My code however shows week 16, which is right. See the caveat?

``  calculateUsersCurrentWorkoutWeek: function(timestamp) {             let daysSinceSignup = moment().diff(timestamp, "days");             if (daysSinceSignup <= 6) {               return 1;             } else if (daysSinceSignup > 6 && daysSinceSignup <= 13) {               return 2;             } else if (daysSinceSignup > 13 && daysSinceSignup <= 20) {               return 3;             } else if (daysSinceSignup > 20 && daysSinceSignup <= 27) {               return 4;             } else if (daysSinceSignup > 27 && daysSinceSignup <= 34) {               return 5;             } else if (daysSinceSignup > 34 && daysSinceSignup <= 41) {               return 6;             } else if (daysSinceSignup > 41 && daysSinceSignup <= 48) {               return 7;             } else if (daysSinceSignup > 48 && daysSinceSignup <= 55) {               return 8;             } else if (daysSinceSignup > 55 && daysSinceSignup <= 62) {               return 9;             } else if (daysSinceSignup > 55 && daysSinceSignup <= 62) {               return 10;             } else if (daysSinceSignup > 62 && daysSinceSignup <= 69) {               return 11;             } else if (daysSinceSignup > 69 && daysSinceSignup <= 76) {               return 12;             } else if (daysSinceSignup > 76 && daysSinceSignup <= 83) {               return 13;             } else if (daysSinceSignup > 83 && daysSinceSignup <= 90) {               return 14;             } else if (daysSinceSignup > 90 && daysSinceSignup <= 97) {               return 15;             } else {               return 16;             }           } ``

Divide by 7 and ceil?

``const daysSinceSignup = moment().diff(timestamp, "days"); return Math.ceil(daysSinceSignup / 7); ``

Did you miss out on coffee this morning?

Note: The function above will tell you "the number of the week since `timestamp`" which is what I understand from your text, but your code sample does not reflect it.

To get the same result of your code sample, you'll need to `.floor()` and add 1 instead of `.ceil()`:

``const daysSinceSignup = moment().diff(timestamp, "days"); return Math.floor(daysSinceSignup / 7) + 1; ``