0

I'm trying to convert the array = ["A", "B", "C", "D"] into array = ["A", "BB", "CCC", "DDDD"]. If you compared the arrays, you'll notice that the letters inside the second array have extra same letters in each position. Here is my code below. I used the for loop because I wanted to use the increment inside the for loop. I don't even know if map() method can do the same so I went with for loop. However, when I checked, it didn't show the result that I wanted. What did I miss?

function accum(s) {
  // your code
  let accArray = s.toUpperCase().split("");

  for (let i = 1; i < accArray.length; i++) {
    accArray[i].repeat(i + 1);
  }

  console.log(accArray);
}
accum("abcd")
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Kristina Bressler
  • 1,642
  • 1
  • 25
  • 60
  • Strings (and all primitives) are immutable. For string sin particular methods like `repeat` and `replace`, etc., return a *new* string with the modified value, the original string remains unchanged. – VLAZ Sep 18 '19 at 06:56
  • @VLAZ Oh right! I completely forgot that `repeat` returns a new string. Thanks for the reminder! – Kristina Bressler Sep 18 '19 at 06:59
  • @VLAZ Why did you mark my question as duplicate? I looked at the other question and it doesn't answer my question. The other question was talking about substr() method while I was asking about repeat() method and map() method. – Kristina Bressler Sep 18 '19 at 07:18
  • String are immutable. Any method that "changes" a string actually returns a new string - `repeat`, `replace`, `substr`, `slice`, `toUpperCase`, and so on and so forth. None of these will change the string they are called on - there is no need to write the same answer for each of these methods when the answer is the same - you have to call the method and assign the value somewhere. – VLAZ Sep 18 '19 at 07:23
  • @VLAZ true that strings are immutable. However, the other question was mostly talking about the solutions and using substr(), slice(), etc. In fact, the other question is a broad question. The quote you mentioned was only one paragraph in the best solution. My question was specific and narrowed to map() and repeat() methods. That is what is different from the other question. – Kristina Bressler Sep 18 '19 at 07:36

5 Answers5

3

A more concise way to achieve the desired output is to use Arrow Functions:

const accum = (s) => s.toUpperCase()
                      .split("")
                      .map((s, i) => s.repeat(i + 1));
                      
console.log(accum("abcd"));
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
2

String.prototype.repeat()

The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

You have to assign the new returned value at the specific index:

accArray[i] = accArray[i].repeat(i + 1);

function accum(s) {
  // your code
  let accArray = s.toUpperCase().split("");

  for (let i = 1; i < accArray.length; i++) {
    accArray[i] = accArray[i].repeat(i + 1);
  }

  console.log(accArray);
}
accum("abcd")
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

You need to push values to array, or assign back to that index, repeat does not mutate the original value it returns a new string Repeat so you need to assign it back

function accum(s) {
  // your code
  let accArray = s.toUpperCase().split("");
  let op = []
  for (let i = 0; i < accArray.length; i++) {
    op.push(accArray[i].repeat(i + 1));
  }

  console.log(op);
}
accum("abcd")
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • I find Mamun's answer to be better, the reason being, that answer utilises existing variables instead of creating a new one. – Krishna Prashatt Sep 18 '19 at 06:58
  • @KrishnaPrashatt i follow rule to not mutate my variables as until unless there a need to, so i used a different variable – Code Maniac Sep 18 '19 at 06:59
1

if your input is already an array like

arr = ['a', 'b', 'c', 'd']

then you can use

arr.map((item, idx) => item.repeat(idx+1))

if you are having string like

str = 'abcd'

then you can use:

str.split('').map((item, idx) => item.repeat(idx+1);
Nutan Gwari
  • 205
  • 4
  • 9
0

You can use forEach or map both work perfectly as iterator

function accum(s) {
  // your code
  let accArray = s.toUpperCase().split("");
  const arr = [];
  accArray.forEach((e, index) => {
    arr.push(e.repeat(index + 1))
  })

  console.log(arr);
}
accum("abcd")
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52