I am trying to write a function with a string as a parameter, and the output should be the same string with each character repeated as many times as the character's index, with capitalizing the first letter.
For example, function accum(abc) should return: A-Bb-Ccc
accum(zytx) should return: Z-Yy-Ttt-Xxxx
I tried the following code but it is not working. Can someone help me out?
function accum(s) {
var strSplit = s.toLowerCase().split('');
var newArr = strSplit.map((element, i) =>
element.repeat(i+1).charAt(0).toUpperCase()+element.substr(1));
console.log("the new Arr is: "+newArr.join('-'));
return newArr.join('-');
}
accum("abcd");
accum('abc') === '-B-CC'.