Reverse A Palindrome - What's Wrong Here?

I was hoping I could have a hand with this algorithm. I can’t see why it’s not working! My checkPal variable works to remove spaces and conform to lower case, and my reverse variable reverses, but the boolean isn’t correct for all of the outcomes.

function palindrome(str) {

  var checkPal = str.toLowerCase().replace(/ /g, "");
  var reverse = checkPal.split("").reverse().join("");

  if (checkPal == reverse) {
  return true;
  }
  else {
  return false;
 }
}

Quote:

You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.

1 Like

Managed to completely miss that! Guess that’s a lesson in reading instructions thoroughly. Got it in the end with this:

function palindrome(str) {
 var checkPal = str.toLowerCase().replace(/[_:(-/,.\s]+/g, "");
 var reverse = checkPal.split("").reverse().join("");
 if (checkPal == reverse) {
 return true;
 }
 else {
 return false;
 }
}
1 Like

Also you can shorten this bit:

 if (checkPal == reverse) {
 return true;
 }
 else {
 return false;
 }

like this:

return checkPal === reverse;

3 Likes

Thanks, that’s a good example of where to use strict equality.

heck, you can shorten it even more:

function palindrome(str) {
  str = str.replace(/[\W\_]/g, '').toLowerCase();
  return str === str.split('').reverse().join('');
}
2 Likes

And shorten even more, :wink:

function palindrome(str) {
  return (str = str.toLowerCase().replace(/\W|_/g, '')) === str.split('').reverse().join('');
}

palindrome("0_0 (: /-\\ :) 0-0");
4 Likes

I’ve really been enjoying going back through the challenges and refactoring them as tight as I can.

That seems so obvious now that you posted it… It’s amazing how you can refactor some things that much.

indeed but it’s a bad practice in production code :tongue:

3 Likes

very true. I would hate to come to a code base after some guy went through and played code golf with it all.

So good practice in a real world situation would be to use an if / else, to make it as easy as possible to understand?

yes… do whatever in your ability to make it as simple as possible for the love of your future self or your future unknown co-worker/developer improving maintainability.

4 Likes

what [\W_] means? And what the difference between that and [W_]? I am asking because with the first one my code runs but with the second one is not

During debug I commented main conditions “if else” and use “return a” for test. I faced main troubles with RegExp, but then got to https://regex101.com/tests and found all required info here.

My version of this task. I divided each step into simple command.

function palindrome(str) {

var str2 = str.toLowerCase();
var a1 = str2.replace((/[_:(-/,.\s]+/g),(’’));

var a = a1.split(’’);
var r = a.reverse();
var j = r.join(’’);

if (a1===j){return true;}
else return false;

}

I’m also interested in learning how that underscore at the end functions

function isPalindrome(str) {
str = str.replace(/\W/g, ‘’).toLowerCase();
return (str == str.split(’’).reverse().join(’’));
}

I was feeling a little bummed out when I wrote my own palindrome function and after testing that it worked I decided to see how other people approached writing this function.

Although the exercise suggested reading about String.prototype.replace(), I was unable to make heads or tails from the MDN documentation. I didn’t understand what I was supposed to do with the 2nd parameter newSubstr as shown below.

str.replace(regexp|substr, newSubstr|function)

So basically, I wanted any character that my regexp matched to be a part of my newSubstr, but the documentation made no sense to me and I abandoned using str.replace in favor of String.prototype.match()

str.match(regexp)

Still feel bummed by the girth of my function code compared to other’s refactored code in this thread. I wanted to test the performance of our functions and discovered using jsperf. To my amazement, the code I wrote turned out to be the faster of the other approaches which helped eased the way I felt about my code.

I do not know how long jsperf keeps the test cases around, but here is a link https://jsperf.com/kerry-ruddock1 for those interested in running the test and checking the results