These are chat archives for camperbot/bonfire-check-for-palindromes

30th
Sep 2015
Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [1/8]

## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [2/8]

Explanation:

You have to check if the string you get as an input is a palindrome and return true if it is, and false otherwise.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [3/8]

Hint: 1

You may use regular expressions (RegExp) to remove unwanted characters from the string.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [4/8]

Hint: 2

You may use Array.prototype.split and Array.prototype.join methods or for loop.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [5/8]

Hint: 3

You may lowercase the string using String.prototype.toLowerCase.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [6/8]

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:45
hint
CamperBot
@camperbot
Sep 30 2015 17:45

hint [7/8]

First

function palindrome(str) {
  var normalizedStr = str.replace(/[\W_]/g, '').toLowerCase();
  var reverseStr = normalizedStr.split('').reverse().join('');
  return normalizedStr === reverseStr;
}

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Ryan Mills
@ryandrewmills
Sep 30 2015 17:46
hint
CamperBot
@camperbot
Sep 30 2015 17:46

hint [8/8]

Second

function palindrome(str) {
  str = str.toLowerCase().replace(/[\W_]/g, '');
  for(var i = 0, len = str.length - 1; i < len/2; i++) {
    if(str[i] !== str[len-i]) {
      return false;
    }
  }
  return true;
}

Code Explanation:

We use regular expressions to replace any uppercase letter into a lowercase. Then we check if the string is the same as the reversed string using split() to split the original string by characters, then reverse and then join it back together.

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @abhisekp

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Abhisek Pattnaik
@abhisekp
Sep 30 2015 17:47
@ryandrewmills do you need any help?
Ryan Mills
@ryandrewmills
Sep 30 2015 17:48
@abhisekp not understanding the regexp \w_ and why that replaces thepunctution
Abhisek Pattnaik
@abhisekp
Sep 30 2015 17:49
@ryandrewmills it's \W_ not \w_.
The \w means [A-Za-z0-9_]
And \W means the opposite of \w i.e. [^A-Za-z0-9_]
But that will also take the underscore character _ out of the equation. So we've to put it explicitly i.e. \W_
Ryan Mills
@ryandrewmills
Sep 30 2015 17:50
@abhisekp i see.... ahhhhhh I was so close...
Abhisek Pattnaik
@abhisekp
Sep 30 2015 17:51
now the regex will remove anything that is not \w and also the _ (underscore)
@ryandrewmills i suppose the last test case was creating the problem. right?
Ryan Mills
@ryandrewmills
Sep 30 2015 17:52
yes... the 0-0 :) one
Abhisek Pattnaik
@abhisekp
Sep 30 2015 17:53
:P
Ryan Mills
@ryandrewmills
Sep 30 2015 17:53
previously I only replaced the ".,"
Abhisek Pattnaik
@abhisekp
Sep 30 2015 17:54
@ryandrewmills if usable, always rely on character classes. It's the best way to work in regex. :)
explain regex
CamperBot
@camperbot
Sep 30 2015 17:54

:point_right: js regex [wiki]

Links to useful RegEx resources.

See also: :clipboard: Tutorials | :syringe: Testing | :soccer: Games | :newspaper: Blogs | :package: Software

:pencil: read more about js regex on the FCC Wiki

Ryan Mills
@ryandrewmills
Sep 30 2015 17:54
you the man @abhisekp! Thanks for your explanation!
CamperBot
@camperbot
Sep 30 2015 17:54
ryandrewmills sends brownie points to @abhisekp :sparkles: :thumbsup: :sparkles:
:star: 515 | @abhisekp | http://www.freecodecamp.com/abhisekp