Palindrome checker project

Hello,

I just completed the Palindrome checker project successfully, my code passes all the test.

My code:

function palindrome(str) {
  //Remove all non-alphanumeric characters and turn the resulting array into a lower cased string
  let regex = /[A-Za-z0-9]/gi;
  let arr = str.match(regex).join("").toLowerCase();
  //Create two arrays, one original one reversed
  let reversedArr = arr.split("").reverse();
  let originalStringArr = arr.split("");
  //console.log(arr);
  //Check for palindromes
  let count = 0;
  for (let i = 0; i < originalStringArr.length; i++) {
    if (reversedArr[i] === originalStringArr[i]) {
      count++;
      //console.log(count);
    }
  }
  if (originalStringArr.length === count) {
    return true;
  } else {
    return false;
  }
}

palindrome("2A3*3a2");

After passing the tests, I checked the official solutions and obviously noticed that the code could much simpler and more brief. If my code passes the tests, but is not as good as the official solution am I supposed to refactor my code, to make it more in line with the solution?

I think that the main reason why my code is longer is, because I find it easier to first split the code apart into a couple of steps, write that down on paper and then write the actual code.

So is the goal of the projects to write code that would be in line with the official solution or just write any code that you are capable of coming up with on your own, that passes the tests?

Hey @kpav!

I think that your strategy is really good. By coming up with your solution first, then checking and analyzing the official solution you will learn a lot more than by just submitting the solution and moving on when it passes all the tests.

The goal of each project is to teach you something but it’s entirely up to you how much you will learn from it :slight_smile:

Done is better than perfect.

It’s pretty common for developers to get a working solution, get a snack, take a walk, and then look at their code again to see if they can clean it up a bit. That’s a good exercise I think: to re-read your own code and see if you can find places were you’re doing extra work or a bit disorganized. I don’t think that you should worry about working yourself towards any particular solution on every challenge.

There are several things to keep in mind when looking at other solutions:

  • Solutions that you find, including the official freeCodeCamp hints are not always a particularly optimal solution. They’re just some people’s answers.
  • Shorter isn’t always better. “Golf code” (code that is as short as possible) is often less performant and hard to read.
  • Sometimes solutions you find (including the official freeCodeCamp hints) may include tools and techniques that you haven’t learned yet. Sometimes that challenge used to be elsewhere in the curriculum, but more often the solution was just contributed by someone with existing knowledge. If you see something unfamiliar in a solution which makes you want to go off and do some research, that’s great! But don’t feel pressured to always understand all the solutions.
2 Likes

Thank you for your reply. I also think I learn more this way.

Thank you for your thorough reply.

Hi @kpav!

I think your solution looks good.

  • It is easy to read and understand

  • Good descriptive variable names instead of let a,b,c

  • Helpful comments in your code

Keep up the good work!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.