I've almost done the Palindrome challenge, there are two things left

Hello! The code below is ready to use in every case, except two.I can’t figure out how to join all if statements in logical structure(nested statements), I can execute only one statement separately. Please help me to deal with it. And by the way, what’s wrong with “My age is 0, 0 si ega ym”, why it can’t be written backwards?


function palindrome(str) {
  
  var m = str.replace(/\W+/gmi, " ").replace(/[_-]/g, "").toLowerCase();
  var stringLength = m.length;
  var firstChar = m.charAt(0);
  var lastChar =  m.charAt(stringLength - 1);
  var fifthChar = m.charAt(4);   - this is for "almostomla"
  var lastFifthChar = m.charAt(stringLength - 5); - this is for "almostomla"
  var secondChar = m.charAt(1); this is for "My age is 0, 0 si ega ym."
  var lastSecondChar = m.charAt(stringLength - 2); this is for "My age is 0, 0 si ega ym."
if(m){
  if(firstChar == lastChar){
    return true;
  }
  else if(fifthChar != lastFifthChar){
    return false;
  }
  else if(secondChar != lastSecondChar){
    return true;
  }
    else return false;
  }  
  

 
  
}



palindrome("My age is 0, 0 si ega ym.");

To answer your last question, it is because of the “.” which returns false.

palindrome("My age is 0, 0 si ega ym.");

Thanks, but if this var m = str.replace(/\W+/gmi, " ").replace(/[_-]/g, “”).toLowerCase(); executes, there’ll be no “.”

http://prntscr.com/gyc0a3

Try removing the spaces.

1 Like

Thank you very much, there is one thing left “almostomla”.

What is your updated code? It worked when I tried it.

Here’s updated code


function palindrome(str) {
  
  var m = str.replace(/\W+/gmi, "").replace(/[_-]/g, "").toLowerCase();
  var stringLength = m.length;
  var firstChar = m.charAt(0);
  var lastChar =  m.charAt(stringLength - 1);
  var fifthChar = m.charAt(4);
  var lastFifthChar = m.charAt(stringLength - 5);
  
if(m){
  if(firstChar == lastChar){
    return true;
  }
  else if(fifthChar != lastFifthChar){
    return false;
  }
  
    else return false;
  }  
  

 
  
}



palindrome("My age is 0, 0 si ega ym.");

When I run your code above, and also trim the whitespace, I am left with these failing cases:

I think you want to rethink the logic in your approach…

almostomla will return true because you are checking if the first and last characters are the same, which is true in this case.

Instead of comparing individual characters at particular positions, think about ways you can look at the whole string at once and whether it passes the standard rule for a palindrome, i.e. reading the same forwards as backwards.

Your code doesn’t execute after the first if statement.

palindrome(“almostomla”); //returns false

your first check is to look at the first and last character and return true, but this isn’t a great way to check. Check the entire str, and not just the characters.

I think you are way over complicating things:

Once you have your clean, lowercase string, in order to see if it’s a palindrome is enough to check if the reversed string ( you should already know how to reverse a string) is equal to the cleaned string.

No need to programmatically check characters ( eg check if first and last are equal)

1 Like

Said what I did! Just faster!! I like how you also left the OP to refactor :slight_smile:

http://prntscr.com/gyc3ro


function palindrome(str) {
  
  var m = str.replace(/\W+/gmi, "").replace(/[_-]/g, "").toLowerCase();
  var stringLength = m.length;
  var firstChar = m.charAt(0);
  var lastChar =  m.charAt(stringLength - 1);
  var fifthChar = m.charAt(4);
  var lastFifthChar = m.charAt(stringLength - 5);
  
if(m){
  if(firstChar == lastChar){
    return true;
  }
  else if(fifthChar != lastFifthChar){
    return false;
  }
  
    else return false;
  }  
  

 
  
}



palindrome("My age is 0, 0 si ega ym.");

Apologies, I must have been using an earlier version of your code.

Regardless, rethink what you’re comparing. Rather than fixing every test on a case by case basis, once you’ve got the regex right, you only need to compare one thing to pass the challenge…

1 Like

Ok, thank you for your help.Appreciate it.