3

I'm trying to solve this exercise which goal is to give me a string that has to be turned into another string. The characters of the new string are repeated like in the example below.

Example: accum("abcd"); // "A-Bb-Ccc-Dddd"

I wrote the following code:

function accum(s) {
  counter = 0;
  for (var i = 0; i < s.length; i++) {
    return s[i].toUpperCase() + s[i].repeat(counter) + "-";
    counter += 1;
  }
}

When I try to run a sample test such as ZpglnRxqenU I get this error:

Expected: 'Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu', instead got: 'Z-'.

Apparently the problem is linked to the loop which is not working, but I can't figure out why.

5 Answers 5

3

Here's an ES6 one-liner :

const accum = word => word.toLowerCase().split("").map( (letter,index) => letter.toUpperCase() + letter.repeat(index) ).join("-")
  
console.log( accum("ZpglnRxqenU") )

Explanation :

  • word.split("") Start with breaking your string into an array of letters
  • .map( (letter,index) => Iterate each letter, keeping track of the index as you go
  • letter.toUpperCase() + letter.repeat(index) Replace each letter with the transformed value that you return
  • At this point, you have an array of transformed values
  • .join("-") Join everything back to a string with "-" as a separator.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering so quickly!
2

You can combine the use of the methods: String.prototype.split(), Array.prototype.map() and Array.prototype.join():

function accum(s) {
  return s
    .split('')
    .map(function (e, i) {
      return e.toUpperCase() + e.repeat(i);
    })
    .join('-');
}

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

ES6:

const accum = s => s.split('').map((e, i) => e.toUpperCase() + e.repeat(i)).join('-');

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

1 Comment

Ok I'm taking that back
1

You can do this:

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

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

You have an array and fill it with the letters, once filled you join the elements with '-'. You could just add the letters to a string but at the end you would have to trim the trailing '-', which isn't wrong but now you have 2 ways to do this.

Also you don't need the counter variable because i is already counting.

3 Comments

Thank you for the quick reply, but when I try to run your code I have this error:
Expected: 'Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu', instead got: 'Z-Pp-Ggg-Llll-Nnnnn-RRRRRR-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-UUUUUUUUUUU'
Ok I fixed it, I didn't notice the uppercase possibility.
0

With array.reduce:

var str = "abcd";
function accum(str) {
    var arr = str.split("");
    return arr.reduce((m, o, i) => {
        m += (i === 0 ? "" : "-") + o.toUpperCase() ;
        for (var k = 0; k < i; k++) {
            m += o;
        }
        return m;
    }, "");
}

var ret = accum(str);
console.log(ret);

Comments

0

This should work for you. When you return the loop will stop. You need to remove that.

function accum(s) {
  counter = 0;
  sA = s.split('');
  for (var i = 0; i < s.length; i++) {
    sA[i] = sA[i].toUpperCase() + sA[i].toLowerCase().repeat(counter);
    counter += 1;
  }
  return sA.join('-');
}
console.log(accum('ZpglnRxqenU'));

3 Comments

You don't need the s.length == i-1 stuff. Just join the array with "-"
Thank you for answering so quickly! I tried to run your code but I get an undefined as a result. This is the error: Expected: 'Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu', instead got: undefined
@InnSaei Just use return sA.join('-') instead of console.log(sA.join('-')); in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.