InnSaei
InnSaei

Reputation: 51

looping through a string and using repeat method

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.

Upvotes: 3

Views: 121

Answers (5)

rrk
rrk

Reputation: 15846

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'));

Upvotes: 0

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

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'));

Upvotes: 2

Jeremy Thille
Jeremy Thille

Reputation: 26360

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.

Upvotes: 3

Parziphal
Parziphal

Reputation: 6902

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.

Upvotes: 1

Faly
Faly

Reputation: 13346

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);

Upvotes: 0

Related Questions