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.");
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.");
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.
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)
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…