javascript - how can i repeat a string multiple times according to its index in a string - Stack Overflow

I am trying to write a function with a string as a parameter,and the output should be the same string

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
Add a ment  | 

3 Answers 3

Reset to default 5

In 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信