Check for Palindromes (with spoilers)

Check for Palindromes (with spoilers)
0.0 0

#1

Hi everyone! I am working my way through some of the JavaScript challenges, and I think I am starting to understand a lot of it. However, I am a little puzzled about the palindrome challenge – is it just me, were the sections on Regular Expressions missing a lot of necessary details for this challenge? I had to do a lot of searching to find needed modifiers or whatnot, and I still didn’t end up using a lot of the expressions others did. For example, here is a basic solution that I did not write – spoilers! (say that while thinking of Doctor Who):

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

Now for my code – it worked, I passed the challenge, but it seems overly complicated and hardly uses Regular Expressions:

My Code (SPOILER)
function palindrome(str) {
  // Good luck!
  var lowerStr = str.toLowerCase();
  var noScore = lowerStr.replace(/_/g, "");

  var expression = /[\d+\w+]/gi;
  var andCount = noScore.match(expression);  
  var joinCount = andCount.join("");
  
  var splitStr = joinCount.split("");
  var reverseStr = splitStr.reverse();
  var joinStr = reverseStr.join("");
  

  
  if (joinCount == joinStr) {
    return true;
  } else {
    return false;
  }
  
}

Would I be correct in saying that by the looks of the first example, I could have simply stacked most of the dot commands (whatever they are called – .reverse, etc)? They certainly didn’t mention that, but it seems helpful! I’m also confused by the " /[\W_]/g " – I know I used the brackets in mine, but only because I found it elsewhere with no description. In addition, the whole .replace thing seems new. Is there a list of the different characters for Regular Expressions (along with .replace, .match, etc)?
Thank you for taking a peek at my code. Hopefully I can understand it a little bit more and become more efficient.

P.S. here is a link to the challenge guide where I found the comparison code: https://forum.freecodecamp.com/t/freecodecamp-algorithm-challenge-guide-check-for-palindromes/


#2

Yup, Javascript allows you to chain things like this. Notice how you needed to declare a long list of variables whereas the example didn’t need any? This is a ‘functional’ style of programming which is generally seen as a good thing in many cases like this one.