recursive function improvement I am creating routes from an array of menuItems. I have written a recursive function, which works fine but I think the code can be improved. So, I need you guys to review my code and suggest me some changes.

Here is the code:

const accumulator = [];
const renderRoutes = (_menuItems) =&gt; {
  _menuItems.forEach((menuItem) =&gt; {
    accumulator.push(
      &lt;Route
        exact
        path={menuItem.path}
        render={props =&gt; (
          &lt;Scene
            {...props}
            menuItem={menuItem}
          /&gt;
        )}
      /&gt;,
    );
    if (menuItem.children) {
      renderRoutes(menuItem.children);
    }
  });
  return accumulator;
};
Avoid recursion, avoid putting the accumulator out of the function that fills it.

My favourite phrase: Every recursive algorithm can be rewritten without recursion. In worst case, using one (extra) stack.

function renderRoutes(menuItems) {
  let accumulator = [];
  let queue = [];
  queue.concat(menuItems);
  while (queue.length &gt; 0) {
    const item = queue.shift();
    accumulator.push(route(item))
    queue.concat(item.children);
  }
  return accumulator;
}


The route(item) is the same as your &lt;Route....