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

5th
Oct 2015
Bill Kroger
@BillKroger
Oct 05 2015 02:18
hint
CamperBot
@camperbot
Oct 05 2015 02:18

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]

Bill Kroger
@BillKroger
Oct 05 2015 02:18
hint
CamperBot
@camperbot
Oct 05 2015 02:18

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]

Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:19
@BillKroger any issues?
Bill Kroger
@BillKroger
Oct 05 2015 02:20
well I think my code is right but for some reason all of the strings that should return false are not doing so, here's what I have:
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:21
@BillKroger could you pls show the code?
explain format
CamperBot
@camperbot
Oct 05 2015 02:21

:point_right: code formatting [wiki]

Inline code

alt text

This an inline `<paste code here>` code formatting with a single backtick(`) at start and end around the code.

Code Block

``` ⇦ Type 3 backticks and then press [shift + enter ⏎]

<paste your code here>,
then press [shift + enter ⏎]

``` ⇦ Type 3 backticks, then press [enter ⏎]

See also: ☛ How to type Backticks | ☯ Compose Mode | ❄ Gitter Formatting Basics

:pencil: read more about code formatting on the FCC Wiki

Bill Kroger
@BillKroger
Oct 05 2015 02:24
function palindrome(str) {
  // Good luck!
  str = str.replace(/[^w*]/g,'').toLowerCase();
  if(str == str.split('').reverse().join('')) {
    return true;
  } 
  else {
    return false;
  }
}



palindrome("eye");
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:24
@BillKroger Everything is correct just the regex
@BillKroger it's the character class \w
Bill Kroger
@BillKroger
Oct 05 2015 02:25
ok that's good to know. so with the regex, all I want to do is remove the non-alphanumeric characters right?
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:26
@BillKroger yep! The \w is equivalent to [A-Za-z0-9_] (note the underscore character)
@BillKroger so to remove the non-alphanums(and underscore), you write str.replace(/[^\w]/g, '')
@BillKroger but to also remove the underscore, you must explicitly write it as str.replace(/[^\w]|_/g, '')
Bill Kroger
@BillKroger
Oct 05 2015 02:30
ahhh i see, so because I forgot the \ before the w, I was actually referring to just the letter "w"?
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:30
yep!
@BillKroger it's good to test the regex here http://regex101.com
explain regex
CamperBot
@camperbot
Oct 05 2015 02:30

: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

Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:30
@BillKroger :point_up: October 5, 2015 8:00 AM
Bill Kroger
@BillKroger
Oct 05 2015 02:31
haha I'm glad it was just a simple oversight, I was going crazy over this one
thanks for your help and the resources! how do I do the brownie points again?
CamperBot
@camperbot
Oct 05 2015 02:32
if you want to thank someone, put an @ before their name!
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:32
@BillKroger np! But always try it atleast for a day!
just mention me while thanking me :P
Bill Kroger
@BillKroger
Oct 05 2015 02:32
@abhisekp thank you!
CamperBot
@camperbot
Oct 05 2015 02:32
billkroger sends brownie points to @abhisekp :sparkles: :thumbsup: :sparkles:
:star: 537 | @abhisekp | http://www.freecodecamp.com/abhisekp
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:33
@BillKroger anyway, here is another soln.
function palindrome(str) {
  var normalizedStr = str.replace(/[\W_]/g, '').toLowerCase();
  var reversedStr = normalizedStr.split('').reverse().join('');
  return normalizedStr === reversedStr;
}
@BillKroger and here is another one
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;
}
@BillKroger it's always good to check multiple solns from others or solving it in multiple ways after solving it once.
Bill Kroger
@BillKroger
Oct 05 2015 02:39
Yeah you make a good point. So is this common practice for real-world projects, I mean do js professionals usually check with multiple solns before pushing their final code?
Abhisek Pattnaik
@abhisekp
Oct 05 2015 02:42
@BillKroger That's controversial. :P
But we're learning here. And we must learn from others.
And many times, in web dev world, you'll USING rather than CREATING.
So it's always good to get to know the best practices and better algorithms. :)
@BillKroger in pro world anything might go wrong. So it's always best to check any other readily available solns. that are more stable and popular.
Craig Rufener
@crufener
Oct 05 2015 05:14
I am not sure why in the for(...) loop 1 is subtracted from str.length and i < len/2. Why divide by 2?
```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;
}
Abhisek Pattnaik
@abhisekp
Oct 05 2015 07:41
@crufener because it just needs to loop half the time and only compare with the other half.
Monesul Haque
@mones-cse
Oct 05 2015 12:27
hint
CamperBot
@camperbot
Oct 05 2015 12:27

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]

Monesul Haque
@mones-cse
Oct 05 2015 12:28
hint make
CamperBot
@camperbot
Oct 05 2015 12:28

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]