0

I have an array of colors that I need to recursively get every nth element to generate a legend. The colors array might look something like this:

[
'red',
'dark_red',
'dark_dark_red',
'green',
'dark_green',
'dark_dark_green',
'blue',
'dark_blue',
'dark_dark_blue'
]

I have a variable amount of legend items that I want to generate colors for. I want to be able to generate the colors based off of the index when looping through the legend items in a pattern like so:

1 - green
2 - blue
3 - dark_red
4 - dark_green
5 - dark_blue
6 - etc...

How might I go about this? I tried using modulus and a double-loop statement but was not able to generate the colors properly.

7
  • Is the index starts with 0 - red? Commented Jul 14, 2020 at 0:28
  • 1
    If the list length is variable and n can be arbitrarily chosen, you can get repeating loops, like if you choose every 3rd element from your 9-item list, you'll just get the 'dark_dark_x' colors. What behavior would you want in that situation? Commented Jul 14, 2020 at 0:34
  • 3
    I don't see how recursion would play into this. I also don't see why a double-loop would play into it either. Modulus would seem the way to go, as in i % amount of colors. Commented Jul 14, 2020 at 0:36
  • Indeed: why do you need to use recursion? Commented Jul 14, 2020 at 0:38
  • Are you basically trying to alternate colors, with a different shade of each base color each time? If so, I think there's an easier way. Commented Jul 14, 2020 at 1:14

2 Answers 2

2

To get the nthItems:

function nthItems(array, n){
  const r = [];
  for(let i=n-1,l=array.length; i<l; i+=n){
    r.push(array[i]);
  }
  return r;
}
const a = [
  'red',
  'dark_red',
  'dark_dark_red',
  'green',
  'dark_green',
  'dark_dark_green',
  'blue',
  'dark_blue',
  'dark_dark_blue'
];
console.log(nthItems(a, 2));
console.log(nthItems(a, 3));

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

2 Comments

I'm sorry, I'm afraid I was unclear in my initial question. I know how to get every nth element, but I need to be able to get every nth element, then move onto the next "subsection" of the data, which is every nth item starting from index 1. Then after that, every nth item starting from index 2.
The first index is really 0 in an Array. "subsection"? What do you mean by that?
0

StackSlave's answer is the most efficient way but if you want a recursive way you could do

const a = [
  'red',
  'dark_red',
  'dark_dark_red',
  'green',
  'dark_green',
  'dark_dark_green',
  'blue',
  'dark_blue',
  'dark_dark_blue'
];

const nthItems = (array, n) => {
  const results = [];
  const addItem = index => {
    if (index < array.length) {
      results.push(array[index]);
      addItem(index + n);
    }
  };
  addItem(0);
  return results;
};

console.log(nthItems(a, 2));
console.log(nthItems(a, 3));

But this is recursive purely for the sake of recursion, it adds values to the stack unnecessarily when a for loop will do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.