June 5th Codewar - 2

·

2 min read

I guess this will count as my second codewar of the day! I feel so proud of myself because I saw the problem and I was able to pseudo code what I needed to do first. The problem was to create a function that will double each character of the string.

The first thought was that I needed to split the string in order to work with each of the characters. I would then need to duplicate or add the same character. After that I would need to join them back.

I do use console.log every time I try something to check if it even works. This was my first part of the code.

function doubleChar(str) {
  // Your code here
  const newstr = str.split("")
console.log(newstr)
}

I then started to get stuck. I knew I had to add or double the character. I searched and found .repeat() method. I had never used it so I looked up a few examples. I then tried it like this

function doubleChar(str) {
  // Your code here
  const newstr = str.split("").repeat()
console.log(newstr)
}

It did not work because I wasn't going through each of the characters. In order to do that I used .map so it looked like this.

function doubleChar(str) {
  // Your code here
  const newstr = str.split("").map(char => char.repeat()).join("")
console.log(newstr)
}

Again it did not pass the test. I think I wrote out repeat wrong because I need to put in the number of times I want it to repeat. Right? I ended up going back after looking at a different way to do it. I ended up just concatenating, then joining at the end.

function doubleChar(str) {
  // Your code here
  const newstr = str.split("").map(char => char + char).join("")
  return newstr

  //console.log(newstr)
}