Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Aug 17 09:33
    User @bjorno43 unbanned @linkin-park
  • Jun 20 21:19
    @bjorno43 banned @shenerd140
  • May 10 09:13
    @bjorno43 banned @zhaokunhaoa
  • Apr 25 17:12
    @mstellaluna banned @cmal
  • Apr 01 18:11
    thecodingaviator labeled #31179
  • Apr 01 18:11
    thecodingaviator labeled #31179
  • Apr 01 18:09
    thecodingaviator commented #31179
  • Apr 01 18:09
    thecodingaviator labeled #31182
  • Apr 01 18:09
    thecodingaviator labeled #31182
  • Apr 01 18:07
    thecodingaviator commented #31182
  • Apr 01 18:07
    thecodingaviator labeled #31190
  • Apr 01 18:07
    thecodingaviator labeled #31190
  • Apr 01 18:07
    thecodingaviator commented #31190
  • Apr 01 18:05
    thecodingaviator commented #31157
  • Apr 01 18:04

    thecodingaviator on master

    remove A (#31157) (compare)

  • Apr 01 18:04
    thecodingaviator closed #31157
  • Apr 01 18:04
    thecodingaviator closed #33788
  • Apr 01 18:04
    thecodingaviator commented #33788
  • Apr 01 18:02
    thecodingaviator closed #31059
  • Apr 01 18:02
    thecodingaviator commented #31059
Saif Ur Rahman
@saifsmailbox98
CamperBot
@camperbot
:bulb: to format code use backticks! ``` more info
Nick Cleary
@Hijerboa
can someone tell me why my code isn't working?
function palindrome(str) {
  str.toLowerCase();
  var workingString = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
  var splitup = workingString.split("");
  var reversed = splitup.reverse();
  var joined = reversed.join("");
  var newString = joined;
  if(newString === str){
    return true;
  } else {
    return false;
  }

}
It works for anything without special characters
kumquatfelafel
@kumquatfelafel
@Hijerboa I would avoid having something like that in your replace function. Take a closer look at what's available for regex https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Nick Cleary
@Hijerboa
I tried something like .replace(/[^\w\s]/gi, '') which should replace all special characters will null I believe but that didn't work either
Coy Sanders
@coymeetsworld
\w includes whitespace
http://regexr.com/ can test out regex here
kumquatfelafel
@kumquatfelafel
@Hijerboa One thing I will say is that strings are immutable. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
As a result, your toLowerCase actually does not change your original string, but returns a new string that you do nothing with.
Moisés Man
@moigithub
\w includes _
soo that regex will KEEP _ @Hijerboa and spaces
Nick Cleary
@Hijerboa
@kumquatfelafel so I should make an new variable with the toLowerCase, like I did with the replace?
@moigithub ah ok thank you
CamperBot
@camperbot
hijerboa sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3439 | @moigithub |http://www.freecodecamp.com/moigithub
kumquatfelafel
@kumquatfelafel
@Hijerboa You don't necessarily need to create a new variable for everything you do. You can also chain your functions or reassign into old variables.
Saif Ur Rahman
@saifsmailbox98
@Hijerboa you are comparing with str, it contains non-alphanumeric characters.
reverse() changes the original array.
toLowerCase() only returns the lowercased vaue.
kumquatfelafel
@kumquatfelafel
For example, since toLowerCase returns a string, you can actually call it then call replace on that returned string immediately afterwards.
str = str.toLowerCase().replace(blahblahblah);
Nick Cleary
@Hijerboa
function palindrome(str) {
  var newString = str.toLowerCase().replace(/[^a-z]/gi, '').split("").reverse().join();
  if(newString === str){
    return true;
  } else {
    return false;
  }

}
this is my new code and it isn't working. I chained the expressions like you said @kumquatfelafel
Saif Ur Rahman
@saifsmailbox98
@Hijerboa [^a-z0-9]
kumquatfelafel
@kumquatfelafel
^. You want to keep numbers as well.
Saif Ur Rahman
@saifsmailbox98
join need a paramater join("")
Nick Cleary
@Hijerboa
fixed those issues... still won't work with anything with a non-alpha numeric character
Saif Ur Rahman
@saifsmailbox98
@Hijerboa you first need to lowercase and replace all non-alphanumeric character from str and assign it back to str, so that later you can compare with it.
kumquatfelafel
@kumquatfelafel

^this as well.

You can also say return newString === str. That's logically equivalent to your if-else return statements.

Nick Cleary
@Hijerboa
@saifsmailbox98 @kumquatfelafel that worked for all but one of them
function palindrome(str) {
  str = str.toLowerCase().replace(/[^a-z]/gi, '');
  var newString = str.toLowerCase().replace(/[^a-z]/gi, '').split("").reverse().join("");

  if(newString === str){
    return true;
  } else {
    return false;
  }

}
Moisés Man
@moigithub
@Hijerboa u need to KEEP numbers too
Saif Ur Rahman
@saifsmailbox98
@Hijerboa :point_up: June 28, 2017 6:15 AM
kumquatfelafel
@kumquatfelafel
@Hijerboa [^a-z0-9]
Nick Cleary
@Hijerboa
facepalm I added that then removed it on accident later
Saif Ur Rahman
@saifsmailbox98
@Hijerboa str is already lowercased and you have already removed non-alphanumeric characters
Nick Cleary
@Hijerboa
thank you guys....
Moisés Man
@moigithub
@Hijerboa if u have already a "cleaned" str.. u can reuse it..
kumquatfelafel
@kumquatfelafel
@Hijerboa you also don't need to do the tolower/replace again :p
Nick Cleary
@Hijerboa
thank you so much @moigithub @saifsmailbox98 @kumquatfelafel
CamperBot
@camperbot
hijerboa sends brownie points to @moigithub and @saifsmailbox98 and @kumquatfelafel :sparkles: :thumbsup: :sparkles:
:cookie: 376 | @kumquatfelafel |http://www.freecodecamp.com/kumquatfelafel
:warning: hijerboa already gave moigithub points
:cookie: 637 | @saifsmailbox98 |http://www.freecodecamp.com/saifsmailbox98
Moisés Man
@moigithub
no need to call replace and tolowercase again
also... this comparison (newString === str return a boolean value true or false..
soo u can directly return the result of that
return newString === str; @Hijerboa
and remove the if
kumquatfelafel
@kumquatfelafel
hahaha... this is what happens when there are too few people asking questions ^^^ :P
Nick Cleary
@Hijerboa
removed the second instance of .replace and .toLowerCase :P
I prob would have punched my computer if it wasn't for you guys. You all rock
kumquatfelafel
@kumquatfelafel
@Hijerboa So... any other questions? Do you want to know how to bake a cake? Simplify your life? Travel back in time? We have those answers and more, and we're all ready and willing to help! :o
Brittani
@Carowyn
Hey, I came back to the perfect conversation...I'm about to attempt the Palindrome one. I'm taking a food break because I sat staring at my computer in confusion lol.
Nick Cleary
@Hijerboa
Yeah one quick question: whats the meaning of life?
One guy told me it was 41 or something