Need help about function palindrome

could you check my code please? thank you in advance
the code is work unless with: palindrome(“almostomla”) and palindrome(“My age is 0, 0 si ega ym.”).

function palindrome(str) {
// Good luck!
var nonAlphaChar = “()`~!@#$ %^&*-+=|{}[]:;”’<>,.?/_";
var result;
for (var i = 0; i < str.length; i++) {

for (var j = 0; j < nonAlphaChar.length; j++) {
  if (str.charAt(i) === nonAlphaChar.charAt(j) || str.charAt(i) === " ") {
    str = str.replace(str.charAt(i), "");
  }
}  
  str = str.toLowerCase();
  for (j = str.length - 1; j >= 0; j--) {
    if (str.charAt(i) === str.charAt(j)) {
      result = true;
   
    }
    else {
    result = false;
    }
  }

}

return result;
}

One very quick way to do this is by using javascript reverse() function that works on array type. So basically you need original string and reversed one and just compare it.

function isPalindrome(){
var original = “anavolimilovana”;
var reversed = original.split("").reverse().join("");
if(original === reversed){
alert(‘Palindrome’);
} else {
alert(‘Not a palindrome’);
}
}

1 Like

This is a good reference for replacing chars: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

thank you mirkoacimovic, but it does not work for palindrome(“0_0 (: /-\ :slight_smile: 0-0”),

I recommend you follow the advice rmdawson71 is giving you; I don’t believe mirkoacimovic is actually reading the full requirements of the challenge.

In any case this is how I solved that one, let me know if you have questions about it (the link I posted earlier was used as reference for the syntax used to replace non alphanum chars whilst preserving alphanumchars).

function palindrome(str) { str = str.replace(/[^\w]|_/g,"").toLowerCase(); var rts = str.split('').reverse().join(''); return rts === str;}

1 Like

changing function arguments considered bad practise:

var str = 'Eye'
if (isPalindrome(str)) 
  doStuff(str) // str changed from 'Eye' to 'eye' by isPalindrome func!