A function, written without any loop statements, that accepts an array and produces a new array equal to the initial array with ith element (1-based) repeated i times.

computer science

Description

TAIL RECURSION. A function, written without any loop statements, that accepts an array and produces a new array equal to the initial array with ith element (1-based) repeated i times.

I was able to get the solution with for loop, but got stuck without loop. Need help in writing the program without loops

here is the code with loop:

export function stretched(a) {
    const stretchedArray = [];
    for (let [index, item] of a.entries()) {
      for (let i = 0; i < index + 1; i++) {
        stretchedArray.push(item);
      }
    }
    return stretchedArray;
}

I have indulded the test as well


Related Questions in computer science category