cori6
cori6

Reputation: 49

array how can I start a loop from the middle and make it end at the element before the start?

Newbie here! I want to start the loop based on today (ex:"Thursday") and I want to make it stop on "Wednesday"...is that possible? Any help is appreciate! thanks

    let days = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
    ];
    let date = new Date();
    let today = date.getDay();
    let result = [];
    

    for (let i = today; i < days.length; i++) {
      result.push(days[i]);
    }

    return result[index];
  };```

Upvotes: 2

Views: 1411

Answers (2)

ITEnthusiasm
ITEnthusiasm

Reputation: 106

I'm here because I had the same question. By God's grace, I literally just discovered a solution that can be done in a single for loop. It's so simple, I'm surprised I spent so long thinking about it. 😅

The Solution

const array = [...Array(10)].map((_,i) => i); // Create an array with values from 0 to 9
const start = 4;

for (let i = start; i < array.length + start; i++) {
  console.log("Value: ", array[i % array.length]);
}

The Explanation

If you already understand the solution above, then you don't need to keep reading. But if the solution above seemed a little confusing/odd, then hopefully this explanation will help you.

The following can be used to loop through elements from start to finish:

for (let i = 0; i < array.length; i++) {
  console.log("Value: ", array[i]);
}

We can also get the same result if we index the array using the remainder operator (also called the modulo operator).

for (let i = 0; i < array.length; i++) {
  console.log("Value: ", array[i % array.length]);
}

This works because for any integer dividend, N, divided by a larger integer divisor, M, the remainder will always be N. For example, the remainder of 4/7 will always be 4. Similarly, the remainder of 0/7 will always be 0. So although we introduced the remainder operator into the loop, nothing changed.

Now, because of how math works, the remainder will never be larger than the divisor. For an integer divisor, M, the remainder R can only be in the range of 0 to M - 1. See the table below:

Operation Remainder
0 / 3 0
1 / 3 1
2 / 3 2
3 / 3 0
4 / 3 1
5 / 3 2
6 / 3 0

Here, the divisor is 3. As we increment the dividend, the remainder loops from 0 to 3 - 1. And it's this mathematical phenomenon that we can use to loop over an array from any starting point.

If the following can be used to loop over an array from start to finish:

for (let i = 0; i < array.length; i++) {
  console.log("Value: ", array[i % array.length]);
}

Then -- because the remainder operator will "loop us back" to 0 when the dividend (in our case, the index, i) is large enough -- we can also use the following to start anywhere within the array and continue until we reach the previous item:

for (let i = start; i < array.length + start; i++) {
  console.log("Value: ", array[i % array.length]);
}

All we had to do was shift the starting point of the loop. The remainder operator takes care of the rest. Hope this helps.

Upvotes: 5

mykaf
mykaf

Reputation: 1386

For simplicity, you could loop from today to days.length, then again from 0 to today.

let days = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
    ];
    let date = new Date();
    let today = date.getDay();
    let result = [];
    

    for (let i = today; i < days.length; i++) {
      result.push(days[i]);
    }
    for (let i = 0; i < today; i++) {
      result.push(days[i]);
    }

    console.log(result);

Upvotes: 1

Related Questions