1

My problem:

I have an array called "weekdays":

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

Imagine it is Saturday and I want to know how many days it is until Tuesday (obviously 3 days). How can I loop through the array - starting at "Sat" and continue at the beginning until it reaches "Tue"?

My code so far:

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const start = weekdays.indexOf("Sat"); const end = weekdays.indexOf("Tue"); let howManyDays = 0; for (let i = start; i < end; i = (i + 1) % weekdays.length) { howManyDays = howManyDays + 1; } 

However, it seems to be that "howManyDays" is still 0 when I run the code in the console in the browser.

3
  • Oh, I think I see where the problem lies: Since "start" is 5 and "end" is 1, the for loop does not loop at all. Commented Jun 29, 2022 at 23:35
  • Is it important that you use a loop at all? Because you don't have to to achieve this... Commented Jun 29, 2022 at 23:37
  • 1
    @Jack_Hu It's not important to use a loop at all, though I thought that would be the obvious way. Commented Jun 29, 2022 at 23:39

5 Answers 5

3
const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const length = weekdays.length; // 7 const start = weekdays.indexOf("Sat"); // 5 const end = weekdays.indexOf("Tue"); // 1 let numOfDays = 0; // If start comes after end, then we can take the difference between the start and the length, // and add that to the end. if (start > end) { numOfDays = (length - start) + end; // (7 - 5) + 1 // 3 } // If start is before end, then just subtract one from the other. if (start < end) { numOfDays = end - start; } // If the start and end are the same, then it's 0, which is what our variable was initialised as. return numOfDays; // 0 

The only consideration is, whether you want the same day to be 0, as in the example, or 7, if it's next week.

Sign up to request clarification or add additional context in comments.

Comments

3

UPDATE

In order to make this work properly we need to take into account array wrapping - which I didn't in my initial answer.

So I'll just leave here a solution similar to the already existing.

const howManyDaysBetween = (start, end) => { const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const week = weekdays.length; const startDay = weekdays.indexOf(start); const endDay = weekdays.indexOf(end); const howManyDays = startDay > endDay ? (week - startDay) + endDay : endDay - startDay; console.log(`How many days between ${start} and ${end}?: ${howManyDays}`); } howManyDaysBetween("Sat", "Tue") howManyDaysBetween("Tue", "Sat") howManyDaysBetween("Mon", "Sun") howManyDaysBetween("Fri", "Wed")

Comments

2

This loop seems most appropriate to the question asked. Although a bit silly if you run 2 indexOf you already got the distance. just need to substract and module array length. But this approach is good for the loop, because you can just compare the values as you go until you find "Tue"

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const start = weekdays.indexOf("Sat"); const end = weekdays.indexOf("Tue"); let howManyDays = 0; for (let i = start; i != end; i++) { i = i % weekdays.length; howManyDays = howManyDays + 1; } console.log(howManyDays)

2 Comments

Thanks a lot. Oh my, I feel stupid. You and the other answers are right, you just need to substract two indexOfs to get to the answer! But since this is the answer most appropriate to my question (how to wrap around an array), I will accept this one :)!
Just beware of the javascript modulu "bug" because it can give you negative numbers. stackoverflow.com/a/4467559/3807365
0

You can simply use this. Without looping.

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const weekLength = weekdays.length; const startDate = weekdays.indexOf("Wed"); const endDate = weekdays.indexOf("Thu"); const result = (weekLength - startDate + endDate) % weekLength; console.log(result); // -> 1 

Edit: Above example gives 0 (zero) if you put the same day as start and end date. If you want to get 7 instead of 0. you can add a check.

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const weekLength = weekdays.length; const startDate = weekdays.indexOf("Sat"); const endDate = weekdays.indexOf("Sat"); const result = (weekLength - startDate + endDate) % weekLength; console.log(!result ? weekLength : result)

Comments

-1

You're needing to count in a base-7 numbering system. The following code will do it simply for you.

const weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; const start = weekdays.indexOf('Sat'); const end = weekdays.indexOf('Tue') + weekdays.length; const howManyDays = end - start; console.log(howManyDays);

1 Comment

This doesn't work if no wrapping occurs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.