Skip to content

Create a string of specified copies of a given string

I am trying to iterate over the original string 3 times. The result I get is: [“a”,”b”,”c”,”d”,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined]

The correct result should be: [“a”, “b”, “c”, “d”, “a”, “b”, “c”, “d”, “a”, “b”, “c”, “d”]

function makeCopies (str, howmany) {
  let newCopy = [];
   for(let i = 0; i <  str.length * howmany; i++) {
return newCopy;

console.log(makeCopies("abcd", 3))

I have tried many variations but nothing works, this is the closest I got.


JavaScript has a repeat Method on Strings. You can just use "abcd".repeat(3) and you will get “abcdabcdabcd”. If you really want an array of the chars, you can spread the string into an array with [..."abcd".repeat(3)].