# Check for Palindromes: Would a \`\`\` for loop \`\`\` help? Removing non-Alphanumeric Characters in Javascript?

**URL:** <https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336>\
**Category:** General\
**Created:** [December 20, 2016, 4:23am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336 "2016-12-20T04:23:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 20, 2016, 4:23am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/1 "2016-12-20T04:23:56Z")

</div>

Not sure how to get started on this one. I’ve heard a for loop is a good idea.

---

<div class="post-metadata">

**Author:** ![lhafen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lhafen/32/164065_2.png) [@lhafen](https://forum.freecodecamp.org/u/lhafen)\
**Post date:** [December 20, 2016, 4:58am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/2 "2016-12-20T04:58:39Z")

</div>

Yes, I used a for loop in my solution. You’ll want to standardize your string inputs first (change all characters to lowercase, remove anything that’s not alphanumeric). Next, find a way to iterate through the characters and check if the strings match both backward and forward. Anytime you need to iterate through something, a for loop works well 🙂

---

<div class="post-metadata">

**Author:** ![Omegga](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/omegga/32/97940_2.png) [@Omegga](https://forum.freecodecamp.org/u/Omegga)\
**Post date:** [December 20, 2016, 4:59am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/3 "2016-12-20T04:59:33Z")

</div>

If you need to do something with each element of a list, then yes.

There are also easier(high level) ways in JavaScript.For example, you don’t need a loop to replace characters in strings, you just need [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/String/replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) it will do the job for you.

If you don’t know how to get started, consider this situation.You don’t have a computer, just a pen and a sheet of paper, and you’re given two 1000-characters words.How would you proceed to check palindromes ?

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 20, 2016, 5:09am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/4 "2016-12-20T05:09:16Z")

</div>

Omegga that is a great question.

So I understand a `for loop` has three components. How would I use them to find non-alphanumeric characters?

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 20, 2016, 5:18am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/5 "2016-12-20T05:18:51Z")

</div>

So this is how I am starting.

```auto
function palindrome(str) {
  str.replace().toLowerCase();
  var newString;
  // Good luck!
  return true;
}

palindrome("eye");

```

---

<div class="post-metadata">

**Author:** ![lhafen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lhafen/32/164065_2.png) [@lhafen](https://forum.freecodecamp.org/u/lhafen)\
**Post date:** [December 20, 2016, 5:24am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/6 "2016-12-20T05:24:27Z")

</div>

I don’t think you need the replace for changing to lower case, just .toLowerCase works fine.

I found this stackoverflow answer very helpful for removing non-alphanumeric characters, for which I used the replace method.[http://stackoverflow.com/questions/9364400/remove-not-alphanumeric-characters-from-string-having-trouble-with-the-char](http://stackoverflow.com/questions/9364400/remove-not-alphanumeric-characters-from-string-having-trouble-with-the-char)

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 20, 2016, 6:22pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/7 "2016-12-20T18:22:34Z")

</div>

```auto

function palindrome(str) {
  str.replace(/\W/g, ' ');
  str.toLowerCase();
  var newString;
  // Good luck!
  return true;
}

palindrome("eye");

```

This is what I got out of the stackoverflow thing you sent me. Good start? I’m not sure if that function removes the underscores though. I guess I’m just confused as to how much `str.replace(/\W/g, ' ');` actually removes.

---

<div class="post-metadata">

**Author:** ![clockwerkz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clockwerkz/32/78704_2.png) [@clockwerkz](https://forum.freecodecamp.org/u/clockwerkz)\
**Post date:** [December 20, 2016, 6:49pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/8 "2016-12-20T18:49:49Z")

</div>

Yup, that’s a good start. So str.replace(/\W/g, ’ '); let’s look at this line. replace takes two arguments, first what we are “looking” to replace, and what we will replace it with. Your first argument is fine, /\W/g is a regex, and to just break this down real quick… the /\W/ means match anything that is not a letter or a number, and that includes white space. The g is a global flag that basically means don’t stop after it finds one match, keep going until it’s gone through the whole string. The second part of your replace poses a bit of a problem… you’re saying take anything that isn’t a letter or a number and replace it with a whitespace. I think what you want is to get rid of those special characters and not put anything in there… so that “e%ye” would become “eye” and not “e ye”. So change the second argument to just be ‘’. Two single quotes with no space in between.  
Now, with str.toLowerCase(); You have the right idea, you want to change everything to lowercase to simplify the comparison, but the problem is the function toLowerCase() doesn’t actually change the existing string, it creates a new string with everything in lowercase. So after that call, str would actually remain the same. So either you want to create a new String to hold the modified String, or just set it equal to itself like str = str.toLowerCase();  
So you’re halfway there I think! I hope this helps and doesn’t confuse you more. 🙂

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 20, 2016, 7:15pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/9 "2016-12-20T19:15:11Z")

</div>

I just feel like I need to go through how to use basic javascript again. One and done on each exercise doesn’t do it for me. Feeling pretty overwhelmed with my current understanding of coding.

---

<div class="post-metadata">

**Author:** ![clockwerkz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clockwerkz/32/78704_2.png) [@clockwerkz](https://forum.freecodecamp.org/u/clockwerkz)\
**Post date:** [December 20, 2016, 7:31pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/10 "2016-12-20T19:31:05Z")

</div>

I can totally relate to that. That’s how I felt when I first encountered those front end exercises (make a tribute page, portfolio, etc). “Here let’s edit this make believe cat app by adding a picture and manipulating wells with jQuery… ok now go make stand alone webpages.” Boom, right into the deep end of the pool. I went to Udemy and bought a couple of courses on html/css/bootstrap to help me understand what the heck was going on.

I would suggest looking at some Udemy courses, they are all 10 dollars right now. I can suggest a couple that I think would be good javascript beginning courses that’ll get you up to speed a better than these one and done examples. Another great resource is CodeAcademy, they have a free Javascript course you could take. Bookswise if you’re just stumped on how to think like a programmer, there’s a great book I’m reading through called, ironically enough, “Think Like a Programmer.” You can find it on Amazon and check out the reviews. It’s based on C++, but you can follow along and do the examples in Javascript.

Just keep hammering away, even if it feels like you’re not moving forward…persistence really is the key.

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 20, 2016, 7:38pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/11 "2016-12-20T19:38:25Z")

</div>

Really though, I just want to know what goes after `var newString`

---

<div class="post-metadata">

**Author:** ![lhafen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lhafen/32/164065_2.png) [@lhafen](https://forum.freecodecamp.org/u/lhafen)\
**Post date:** [December 20, 2016, 8:21pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/12 "2016-12-20T20:21:48Z")

</div>

For removing non-alphanumeric characters, what you have there does leave the underscores. Just under that on the stackoverflow page they offer an alternative RegEx that will leave just alphanumeric characters in your string: `input.replace(/[^0-9a-z]/g, '')`. The caret means that instead of replacing these characters, 0-9 and a-z will be the only characters left behind.

Like clockwerkz said, you should assign your replace function to the `newString` variable because `replace()` won’t actually alter str.

In my own solution (which works, but may not be the best way) I separated my string characters into an array and used that to check for equivalency. So that was the next step after cleaning up the strings.

---

<div class="post-metadata">

**Author:** ![clockwerkz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clockwerkz/32/78704_2.png) [@clockwerkz](https://forum.freecodecamp.org/u/clockwerkz)\
**Post date:** [December 20, 2016, 8:32pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/13 "2016-12-20T20:32:00Z")

</div>

That’s the regex I used… /[^0-9a-z]0/. I looked up \W and it seems to do the same thing, but when I plugged it in to my solution it didn’t get rid of the underscore. So yeah, maybe just use that replace command instead of the \W.

But before all this, I think you could use a step back and look at something like CodeAcademy to help strengthen the basics down.

Omegga’s suggestion of looking at the problem from a pen and paper point of view is great, too.

---

<div class="post-metadata">

**Author:** ![nameoferic](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nameoferic](https://forum.freecodecamp.org/u/nameoferic)\
**Post date:** [December 21, 2016, 10:35pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/14 "2016-12-21T22:35:39Z")

</div>

Clockwerkz,

I’m jumping into Jonas Schmedtmann’s JS course on udemy and it is working so well. I understand the code infinitely better now! I would recommend it to anyone just learning code or needing a refresher.

---

<div class="post-metadata">

**Author:** ![nickolasteixeira](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nickolasteixeira/32/32602_2.png) [@nickolasteixeira](https://forum.freecodecamp.org/u/nickolasteixeira)\
**Post date:** [January 11, 2017, 12:08am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/15 "2017-01-11T00:08:40Z")

</div>

Here’s a working solution that uses regexp. Only took me 5 hours of regexp research and no experience with replace() methods, but it works! In order to solve this problem, you have to first clean the argument of the non alphanumeric characters and assign it to a new variable. Then you have to create a new variable that ‘palindromes’ if you will. Then you have to test the cleanPalindrome with the ‘palindromed’ Palindrome, if that makes sense. If it matches, return true, if not return false.

function palindrome(str) {

// Clean the palindrome from all the non alphanumeric characters  
var cleanPalindrome = str.replace(/[^0-9a-z]/gi, ‘’).toLowerCase();

// Now I have to ‘palindrome’ the palindromeClean and see if it matches the  
var palindromeReverse = cleanPalindrome.split(’’).reverse().join(’’);  
var newPalindrome = palindromeReverse.toLowerCase();

// Testing the palindrome case  
if (newPalindrome === cleanPalindrome ) {  
return true;  
}  
return false;  
}

palindrome(“five|\_/|four”);

---

<div class="post-metadata">

**Author:** ![Consciously](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/consciously/32/105061_2.png) [@Consciously](https://forum.freecodecamp.org/u/Consciously)\
**Post date:** [January 16, 2017, 9:25am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/16 "2017-01-16T09:25:45Z")

</div>

it works … awesome. Actually you don’t need the ‘toLowerCase()’ method twice, though. After you reverse the word, just compare the two variables ‘cleanPalindrome’ and palindromeReverse with each other and it still works.

---

<div class="post-metadata">

**Author:** ![fuqted](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fuqted/32/35040_2.png) [@fuqted](https://forum.freecodecamp.org/u/fuqted)\
**Post date:** [January 16, 2017, 11:39pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/17 "2017-01-16T23:39:25Z")

</div>

RegExp is cool but without it you can do it like this

```
  str = str.toLowerCase().split("").
  filter(function(x){ return x !== x.toUpperCase() || Number.isInteger(x) });

  //x !== x.toUpperCase() checks whether or not x is a letter

   return str.join("") === str.reverse().join("");

   //for some reason it never works when comparing arrays so you have to join
```

---

<div class="post-metadata">

**Author:** ![jaykt2000](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@jaykt2000](https://forum.freecodecamp.org/u/jaykt2000)\
**Post date:** [January 23, 2017, 10:17pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/18 "2017-01-23T22:17:08Z")

</div>

// This method will work  
function palindrome(str) {  
var re = /[\W\_]/g;  
var result = str.toLowerCase().replace(re, ‘’);

if(result.length \>=1 || result.length === 0) {  
for(var i = 0; i\< result.length; i++) {  
if(result[i] !== result[result.length-1-i]) {  
return false;  
}  
}

}  
return true;

}

---

<div class="post-metadata">

**Author:** ![adam4813](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@adam4813](https://forum.freecodecamp.org/u/adam4813)\
**Post date:** [January 24, 2017, 4:02am UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/19 "2017-01-24T04:02:50Z")

</div>

You really only need to search the half length of the string (+1 if it is an odd number) and then you can return.

---

<div class="post-metadata">

**Author:** ![Zubru22](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zubru22/32/5058_2.png) [@Zubru22](https://forum.freecodecamp.org/u/Zubru22)\
**Post date:** [February 1, 2017, 7:57pm UTC](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336/20 "2017-02-01T19:57:31Z")

</div>

Thank you soo much for that reply! Didn’t had any problems with reversing and comparing strings but getting proper regex was huge problem for me. Thanks to your reply I finally understood what that regex do! 🙂

[Next page](https://forum.freecodecamp.org/t/check-for-palindromes-would-a-for-loop-help-removing-non-alphanumeric-characters-in-javascript/67336.md?page=2)
