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");
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");
Share
Improve this question
edited Aug 26, 2018 at 11:39
Blue
23k7 gold badges66 silver badges98 bronze badges
asked Aug 26, 2018 at 11:27
Elias RubElias Rub
991 gold badge3 silver badges10 bronze badges
1
-
1
Btw, your specification only works if array indexes are 1-based. If they are 0-based, the first character would always be mapped to the empty string (i.e. repeated 0 times). E.g.
accum('abc') === '-B-CC'
. – FK82 Commented Aug 26, 2018 at 12:58
3 Answers
Reset to default 5In functional style (thanks to @Redu for the ment):
const accum = (s) => Array.from(
s,
(c, i) => `${c.toLocaleUpperCase()}${c.repeat(i)}`
)
.join('-');
console.log(accum(''));
console.log(accum('a'));
console.log(accum('xyz'));
Your code doesn't work because in this line
element.repeat(i+1).charAt(0).toUpperCase()+element.substr(1))
element.substr(1)
tries to get the substring starting at index 1 of a single character (which is always the empty string). You probably thought that element.repeat(i+1)
would work in place (i.e. modify element
) whereas it returns a new string (see MDN).
You can fix this as follows:
function accum(s) {
var strSplit = s.toLowerCase().split('');
var newArr = strSplit.map((element, i) =>
`${element.toLocaleUpperCase()}${element.repeat(i)}`);
console.log("the new Arr is: "+newArr.join('-'));
return newArr.join('-');
}
accum("abcd");
The problem is your .charAt(0)
which is always taking the first character, and you're not repeating the lowercase letters afterwards.
function accum(s) {
var strSplit = s.toLowerCase().split('');
var newArr = strSplit.map(
(element, i) => element.toUpperCase()+element.repeat(i));
return newArr.join('-');
}
console.log("the new Arr is:", accum("abcd"));
console.log("zytx:", accum("zytx"));
You can simplify this even further, by removing the .substr()
and just put element.repeat(i)
. On the first iteration, i
will be 0 and return an empty string.
This can be achieved using a simple regex and repeat
too, as in:
function accum(s) {
var i = 0;
return s.replace(/./g, m => "-" + m.toUpperCase() + m.repeat(i++)).slice(1);
}
console.log(accum("abcd"));
The slice(1)
part is used to remove the -
from the start of the string.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745124577a4612614.html
评论列表(0条)