I have a function that is meant to do this:
accum("abcd"); // "A-Bb-Ccc-Dddd"
accum("RqaEzty"); // "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
We can see the first "for" loop repeats each substring by it's current (index + 1)
. It pushes it to the array and the output would be [ 'a', 'bb', 'ccc', 'dddd' ]
It is then obvious that I need to iterate over this array and capitalise each string which I have done by the second for loop below.
The problem is when I return the array it is returning like this: [ 'A', 'B', 'C', 'D' ]
It is returning the first substring of each string but it isn't returning the rest of them.
function accum(s) {
var splitstring = s.split("")
var newarray = []
for(var i = 0; i < splitstring.length; i++) {
newarray.push(splitstring[i].repeat(i + 1))
}
for (var i = 0; i < newarray.length; i++) {
newarray[i] = newarray[i].charAt(0).toUpperCase()
}
return newarray
}
accum("abcd")