2
function palindrome(str) {
  str = str.replace(' ', '');
  str = str.replace(',', '');
  str = str.replace('.', '');
  str = str.toLowerCase();
  if (str.length % 2 === 0) {
    var x = 0;
    while (x < (str.length - x)) {
        if (str.charAt(x) === str.charAt((str.length - x) - 1)) {
          x++;
        } else {
          return false;
        }
   }
   return true; 
  } else {
    var y = 0;
    while (y < (str.length - y - 1)) {
      if (str.charAt(y) === str.charAt((str.length - y) - 1)) {
          y++;
        } else {
          return false;
        }
   }
   return true;
   }
}

palindrome("eye");

This may not be the most effecient way of solving this, but I begin by remove extraneous characters, then I used an if/else to split out even and odd string lengths. Within each, I only check equality of characters up through the middle of the word - since past that would be repetitious.

However, after multiple changes and looking into other solutions for the problem, I still cannot get mine to pass for a particular case: palindrome("never odd or even")

If it helps, it passes for "race car" and "almostomla" and "eye".

Thanks in advance!

3 Answers3

2

The problem is that the native replace Javascript function is only replacing a single occurrence in the string. Use Regex to account for all of the matches within the string.

However, keep in mind that the "." character is used in Regex as a wildcard so you need to escape it with a backslash to tell Regex you're specifically looking for the "." character. See this JSFiddle as an example: https://jsfiddle.net/on333yf9/3/

function palindrome(str) {
  str = str.replace(/ /g, '');
  str = str.replace(/,/g, '');
  str = str.replace(/\./g, '');
  str = str.toLowerCase();
  if (str.length % 2 === 0) {
    var x = 0;
    while (x < (str.length - x)) {
        if (str.charAt(x) === str.charAt((str.length - x) - 1)) {
          x++;
        } else {
          return false;
        }
   }
   return true; 
  } else {
    var y = 0;
    while (y < (str.length - y - 1)) {
      if (str.charAt(y) === str.charAt((str.length - y) - 1)) {
          y++;
        } else {
          return false;
        }
   }
   return true;
   }
}
My Stack Overfloweth
  • 4,729
  • 4
  • 25
  • 42
1

The problem is because of the following lines:

str = str.replace(' ', '');
str = str.replace(',', '');
str = str.replace('.', '');

It does replace all white spaces, commas or dots globally, it just replaces one space, comma and dot, if it's there. You have to find all spaces, commas and dots and remove them. This is what you can do,

str = str.replace(/ /g, '');
str = str.replace(/,/g, '');
str = str.replace(/./g, '');

The g character means to repeat the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here.

Edited:

You can do something like this:

if(str.replace(/ /g, '').length != 0){
    str = str.replace(/ /g, '');
}
if(str.replace(/,/g, '').length != 0){
    str = str.replace(/,/g, '');
}
if(str.replace(/\./g, '').length != 0){
    str = str.replace(/\./g, '');
}
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • This definitely helps! However, upon making that change in the code, I now fail for `"almostomla"` and all others that are non-palindrome strings. – matthew_t_smith Nov 25 '15 at 18:42
  • @matthew_t_smith see the **edited** section of my answer. – Rajdeep Paul Nov 25 '15 at 19:08
  • @matthew_t_smith oops, I almost forgot that you also need to escape dot(`.`)character with a backslash so that regex could look for dot(`.`). Edited that also. Now it should work fine. – Rajdeep Paul Nov 25 '15 at 20:06
0

Unless you want to write your own code why not user reverse/join?

function palindrome(str) 
{
  str = str.split(' ').join('');
  str = str.split(',').join('');
  str = str.split('.').join('');
  str = str.toLowerCase();
  if (str.split('').reverse().join('') == str)
  {
    return true;
  }
  else
  {
    return false;
  }

}

palindrome("never odd or even");
Sergio S
  • 194
  • 7
  • Mostly, at this point, I'm just trying to power through fixing this set of code. I realize it's roundabout, but by now I can't give up. – matthew_t_smith Nov 25 '15 at 18:45