How to learn best practices (Check for Palindromes Challenge)

How to learn best practices (Check for Palindromes Challenge)
0.0 0

#1

Tell us what’s happening:
After building my code for the Palindrome challenge, I was grateful to have finished it but frustrated that I was quite different than the basic/ intermediate solutions offered in the help section - and way off from the advanced solution. I’ve added the advanced code below my solution.

My question is how do I approach solving problems in a way that helps me to learn best practices and avoid creating bad habits (or bad code) early on?

Your code so far

function palindrome(str) {
  var string = str;
  string = string.toLowerCase().replace(/\W+/g,"").replace(/\s+/g,"").replace("_","");
 
  var newString = string.split("");
 
  if (newString.join() === newString.reverse().join()) {
    return true;
  }
  return false;
  
}

palindrome("1 eye for of 1 eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:

//advanced example

function palindrome(str) {
// make all letters lowercase and remove non-alphanumeric characters
str = str.toLowerCase().replace(/[^a-z|1-9]/g, “”);

// if the length of the string is 0 then it is a palindrome
if (str.length === 0){
return true;
}
// if the first letter and the last letter of the string
// do not equal each other, then it is not a palindrome
if (str[0] !== str[str.length-1]){
return false;
}
//Else, run the function without the first and last characters.
else{
return palindrome(str.slice(1,str.length - 1));
}
}

palindrome(“mooo^&^%$m”);


#2

Think about how you approach programming problems: https://medium.com/learn-love-code/stuck-on-a-coding-problem-here-are-5-steps-to-solve-it-8be04c4b4f19

Your solution is good. Honestly, I like your solution more than the advanced example. Your code is way easier to read. Readability is important, especially when more than one developer is working on a project.

You do create a lot of variables when you don’t need to. This is your code refactored:

function palindrome(str) {
  str = str.toLowerCase()
    .replace(/\W+/g,"").replace(/\s+/g,"").replace("_","")
    .split("");
 
  if (str.join() === str.reverse().join()) {
    return true;
  }
  return false;
}



palindrome("eye");

#3

Thank you @elisecode247 for taking the time to respond. I love your refactored code - it makes a ton of sense. :slight_smile: I don’t know why I feel a need to create more variables than needed - but now that I’m aware I can work to improve it.

I enjoyed the article as well and will implement the 5 steps into my learning :+1:


#4

Taking @elisecode247’s solution, it can be refactored again to create a single replace and remove the if statement.

function palindrome(str) {
  str = str.toLowerCase()
    .replace(/[\W_]/g,"")
    .split("");
  return str.join() === str.reverse().join(); 
}

When you post full working solutions, it is always best to surround your code with [spoiler] and [/spoiler] on the lines before and after your code. That way someone who finds your post looking for hints and not full working solutions, will not see them accidentally.


#5

Thank you @randelldawson - I’m loving the challenge of learning code and find beauty in simplicity. I really appreciate you taking time to respond.

Thanks for the advice with [spoiler] as well!