# Need help about function palindrome

**URL:** <https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404>\
**Category:** General\
**Created:** [March 27, 2017, 9:42am UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404 "2017-03-27T09:42:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pprovi](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@pprovi](https://forum.freecodecamp.org/u/pprovi)\
**Post date:** [March 27, 2017, 9:42am UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/1 "2017-03-27T09:42:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mirkoacimovic](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mirkoacimovic/32/45427_2.png) [@mirkoacimovic](https://forum.freecodecamp.org/u/mirkoacimovic)\
**Post date:** [March 27, 2017, 8:15pm UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/3 "2017-03-27T20:15:38Z")

</div>

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’);  
}  
}

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [March 27, 2017, 8:41pm UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/5 "2017-03-27T20:41:35Z")

</div>

This is a good reference for replacing chars: [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)

---

<div class="post-metadata">

**Author:** ![pprovi](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@pprovi](https://forum.freecodecamp.org/u/pprovi)\
**Post date:** [March 28, 2017, 9:45pm UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/6 "2017-03-28T21:45:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [March 28, 2017, 10:23pm UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/7 "2017-03-28T22:23:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![0Prime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/0prime/32/160620_2.png) [@0Prime](https://forum.freecodecamp.org/u/0Prime)\
**Post date:** [April 1, 2017, 10:14am UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/8 "2017-04-01T10:14:27Z")

</div>

changing function arguments considered bad practise:

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:25pm UTC](https://forum.freecodecamp.org/t/need-help-about-function-palindrome/100404/9 "2021-01-16T17:25:38Z")

</div>

Closed on January 16, 2021, 5:25pm UTC


