0

I take on input string "ZpglnRxqenU"

and must return string with that view "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu"

i almost done, but i don't know why first letter in array elements don't change to upper case.

function accum(s) {
  let str = s.toLowerCase();
  let arr = [];
  
  for(let i=0; i<str.length;i++){
      arr.push(str[i].repeat(i+1));
      arr[i][0].toUpperCase();
  }
  
  return arr.join('-');
}



console.log(accum("ZpglnRxqenU"));

// must be "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu"
Drop
  • 1,394
  • 3
  • 15
  • 25
  • 2
    As a side note you can write a one-liner like `'ZpglnRxqenU'.split('').map((el, i) => el.toUpperCase() + el.repeat(i).toLowerCase()).join('-')` – Vinz243 Oct 20 '18 at 13:25
  • @connexo: Your example in your comment above (`arr[i][0]=arr[i][0].toUpperCase()`) won't work because you are also trying to modify the string. – Teemoh Oct 20 '18 at 13:42
  • @Teemoh Indeed, that's why I changed it to `arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1);` in my answer. Couldn't modify the comment quick enough, though. – connexo Oct 20 '18 at 13:44

4 Answers4

2

Strings are immutable and just using .toUpperCase() will not update existing string, to do that you can try below code

function accum(s) {
  let str = s.toLowerCase();
  let arr = [];

  for(let i=0; i<str.length;i++){
      arr.push(str[i].repeat(i+1));
      var [firstLetter, ...rest] = arr[i]
      arr[i] = firstLetter.toUpperCase() +rest.join('')
  }

  return arr.join('-');
}
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22
  • This requires the use of Babel to be compatible with IE 11 and lower. – connexo Oct 20 '18 at 13:22
  • @connexo yes, for object destructuring support complier is required in IE. But is it not ok to make use of latest features and use complier for IE, also full support is already there in Chrome, any thoughts that may be helpful for me for my upcoming answers? – Nitish Narang Oct 20 '18 at 13:30
  • 1
    Ofc it is okay, but it should be noted when applied on a IE11-compatible-code-based question. – connexo Oct 20 '18 at 13:33
  • Thank you @connexo, though I am not sure question mentioned "E11-compatible-code-based question" anywhere. Nonethless thanks. – Nitish Narang Oct 20 '18 at 13:38
  • It didn't mention that, but code was IE11-compatible to start with. So suggesting a solution that ends this compatibility is perfectly fine, but helpful to at least mention that. – connexo Oct 20 '18 at 13:38
1

Strings in Javascript are immutable, so it's got to be arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1) if you want to convert it.

toUpperCase() only returns the character(s), it won't manipulate the String it was called on.

The same is true for any other method on String.prototype like e.g. String.replace().

To make it even more clear: The only way in Javascript to ever change a variable containing a String is assigning a new value to the variable.

function accum(s) {
  let str = s.toLowerCase();
  let arr = [];
  
  for(let i=0; i<str.length; i++){
      arr.push(str[i].repeat(i+1));
      arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1);
  }
  
  return arr.join('-');
}



console.log(accum("ZpglnRxqenU"));
connexo
  • 53,704
  • 14
  • 91
  • 128
  • yea, i try to with that way `arr[i][0]=arr[i][0].toUpperCase()`, but it anyway was return strings with lower case. http://jsfiddle.net/51pfj3gc/5/ – Drop Oct 20 '18 at 13:28
  • 1
    Yes, because that was another attempt to treat the String as an array and modify part of it (`arr[i][0]`) without reassigning the **variable**. Corrected. – connexo Oct 20 '18 at 13:35
1

You can to do it like this way and remove unnecessary this line arr[i][0].toUpperCase();

function accum(s) {
  let str = s.toLowerCase();
  let arr = [];
  
  for(let i=0; i<str.length;i++){
      let gStr = str[i].repeat(i+1);
      arr.push(gStr[0].toUpperCase() + gStr.slice(1));
  } 
  return arr.join('-');
}
console.log(accum("ZpglnRxqenU"));
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

You could take the power of Array.from and get the string in characters and map the first upper case character and the repeated characters for a new string.

At the end join all items to a single string.

function accum(s) {
    return Array
        .from(s.toLowerCase(), (c, i) => c.toUpperCase() + c.repeat(i))
        .join('-');
}

console.log(accum("ZpglnRxqenU"));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392