Check for Palindromes returning only true or false

Tell us what’s happening:

Your code so far

function palindrome(str) {
  // Good luck!
       t = str.toLowerCase();
       x = t.split("");
       y = x.reverse();
       z = y.join("");
       regExp = z.replace(/[\W_]/gi, "");
       if(regExp === str)
           return true;
           return false;


**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/60.0.3112.78 Chrome/60.0.3112.78 Safari/537.36```.

**Link to the challenge:**
1 Like

Let me help you break down what your code is doing for a specific test which you are failing:


See the comments to the right of each step below in your code:

function palindrome(str) {
  // Good luck!
       t = str.toLowerCase(); //  t = "_eye"
       x = t.split(""); // x = ["_", "e", "y", "e"]
       y = x.reverse();  // y = ["e", "y", "e", "_"]
       z = y.join("");   //  z = "eye_"
       regExp = z.replace(/[\W_]/gi, ""); // regExp = "eye"

       if(regExp === str) // you are asking if "eye" is equal to "_eye" because str never changed
           return true;
           return false; // because the if statement condition evaluated to false, false is returned

Now you should have a better idea of what to compare. If you need any more assistance, just ask and someone will respond.

one of function call requires _eye… and why that one is eliminated in regExp.

I cant understand that yet?

If that underscore is removed , how it will be true. it is not possible though.

In the challenge instructions there is the following note:

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

So, one of the first things you need to do is remove all all non-alphanumeric characters first. This means characters which are not 0 thru 9 and which are not the letters A thru Z and not the letters a thru z. The following line removes the non-alphanumeric characters from the variable z and assigns them to regExp, but the replace needs to occur after the str.toLowerCase and before the split statement.

RegExp = z.replace(/[\W_]/gi, "");

Hey Rmdawson71,

I can understand that non alphanumeric instruction part,

But what i dont understand is _eye needs to be returned as true, as it is one of the challenge condition.

so when I pass the string “_eye” the replace function will eliminate the _ with regExp characters.

So how come the “_eye” condition will return true? because the underscore is eliminated with regExp

I hope you understand my question

Mean while the my code is running , but i dont feel this is a correct form of code. check and tell me.

function palindrome(str) {
   low = str.toLowerCase();
   regExp = low.replace(/[\W_]/g, "");
   s = regExp.split("");
   r = s.reverse();
   j = r.join("");
   if (regExp == j)
     {return true;}
   else {return false;}


So how come the “_eye” condition will return true?

Based on the following definition in the challenge of a palindrome:

A palindrome is a word or sentence that’s spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

So, we can only determine if the characters are the same forwards and backwards if we ignore the punctuation and spaces, so that is why we replace them with blank strings before try to make any comparisons.

“_eye” becomes “eye” which should return true, because it spells the same forwards or backwards.

Does this answer your question?

As far as correct code form, I would make the suggestion to name your variables in such a way as they describe the data they hold. It makes it easier for others to read what your code is trying to do. For example, if you rewrote your solution as:

function palindrome(str) {
   lowerCaseStr = str.toLowerCase();
   cleanStr= lowerCaseStr.replace(/[\W_]/g, "");
   strArray = clearnStr.split("");
   reverseArray = strArray.reverse();
   reverseStr = reverseArray.join("");
   if (cleanStr== reverseStr)
     {return true;}
   else {return false;}


Another approach using the same logic is to chain the functions together with “.” and let the function names explain what is going on instead of using variable names:

function palindrome(str) {
   cleanStr = str.toLowerCase().replace(/[\W_]/g, "");
   reverseStr = cleanStr.split('').reverse().join("");
   if (cleanSt == reverseStr)
     {return true;}
   else {return false;}


Thank you for helping me out :slight_smile:

This displays whether the string is a palendrome or not

Click Me!

Did you mean to put a link?

const numeric = "1234567890";
const alphaLower = "abcdefghijklmnopqrstuvwxyz";
// constant time complexity, O(1),
// since the number of numeric, alphabets are constant
function convertToAlphaNumeric(c){ 
    var indexUpper = alphaUpper.indexOf(c);
    if(indexUpper >=0 )
        return alphaLower(index);

    if(alphaLower.indexOf(c) >=0 )
        return c;

        return c;

    return '';
function palindrome(str) {
 // shallow copied into new memory of length of str 
    var arr = Array.from(str, x=> convertToAlphaNumeric(x) );
 // to prevent copying whole array into new memory space
 // for every changes, used 2 pointers and change 
 // the value in existing (original) array, "arr".  
    var j=0; 
    for(var i=0; i<arr.length; i++){
        if(arr[i] != ''){
            arr[j++] = arr[i];
    arr.length=j; // resize array without any consuming extra memory   

    // by using 2 pointers, p1 starts from 0 and moving forward, 
    // p2 starts from the end of the array, moving backward,
    // so that we will need to look up only half length of the array
    var p1 = 0; 
    var p2 = arr.length -1;
    while(p1 < p2){
        if(arr[p1] != arr[p2]) return false;
    return true;