Everyone Can Code!

JavaScript Kata #4: Remove First and Last Character


If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.

If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.

Kata #4

DESCRIPTION:

It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters.

Starting code:

function removeChar(str){
 //You got this!

};

My attempt that worked:

function removeChar(str){
 //You got this!
  let newStr = ""
  for(i = 0 ; i < str.length ; i++){
    if(i == 0){
      newStr += ""
    }else if(i > 0 && i < (str.length-1)){
      newStr += str[i]
    }else if (i == str.length){
      newStr += ""
    }
  }
  return newStr
};

Have to admit, it took several attempts to arrive at a solution that worked. As I've mentioned in Kata #2, I knew that I can iterate over each character in the string like an array but got lost in thinking I could immediately use array methods to the string. Had to scroll a bit to see that my approach is ranked #10 so definitely a sign that I could improve on a lot. Upside is that I learned a lot today about the other string methods I could have used to arrive at the solution faster, starting with .slice being used by the ranked #1 & #2 'Best Practice', with #2 varying by creating an anonymous/arrow function instead of using the original starting code:

function removeChar(str) {
  return str.slice(1, -1);
}

Also learned about the .substring method from rank# 3:

function removeChar(str){
 return str.substring(1, str.length-1);
};

Pretty pleased to know that I had the right idea by using the .shift and .pop array methods when I saw rank #4 but I didn't know .split and .join at the time so my attempt didn't work, and I'm glad I got to see the right way to do it:

function removeChar(str){
 //You got this!
  str1 = str.split('');
  str1.shift();
  str1.pop();
  return str1.join('');
    
};

Ranks #5 to #8 were also using .slice and .substring but rank #9 used the .replace method which I should've learned from Kata #3 by now but for some reason my brain didn't default to going in that direction:

const removeChar = (str) => str.replace(/^.|.$/g, '');

And there you have it folks. I guess failures and shortcomings really do teach a lot more to a person than wins. Hopefully I get comfortable with these new methods so I can utilize them in future coding challenges.

That's it for Kata #4, stay tuned for more katas to be solved!

Link to Kata #1: Square(n) Sum

Link to Kata #2: Convert a Number to a String

Link to Kata #3: DNA to RNA Conversion

How do you rate this article?

2


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.