# 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;
}

For palindrome(“almostomla”), take a look below at line #12:

``````  for (j = str.length - 1; j >= 0; j--) {
``````

Put the following below your line #12, so you can see the values of what you are comparing during each iteration in the for loop. You are making too many comparisons.

``````console.log(str.charAt(i),str.charAt(j));
``````

In general, your loop logic is not correct. Write out the algorithm without any code first and post it here and let’s first make sure your logic is correct, before trying to code it in javascript.

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){
} else {
}
}

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 (: /-\ 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!``````