239 Check for Palindromes (Pls Help me refactor my answer)

239 Check for Palindromes (Pls Help me refactor my answer)
0

#1

First off im STOKED i got this basically on my own.

I would just like some help refactoring where its needed. I would look at someone elses code but i like to see specific examples pertaining to my code. It just sinks in better for me. Thanks for any responses.

function palindrome(str) {
 
  var oldStr = str.toString().replace(/[^0-9a-zA-Z]/g, '').toLowerCase().split("").join().replace(/[^0-9a-zA-Z]/g, '');
  var newStr = oldStr.split("").reverse().toString().replace(/[^0-9a-zA-Z]/g, '');
  
  if (newStr === oldStr){
    return (true);
  }else{
    return (false);
  }
}

palindrome("0_0 (: /-\ :) 0-0");

**It felt weird calling .replace 3 different times.


#2

You only need to call replace() once.
If you use the /i flag on your regex it means “ignore case” and you don’t have to use both a-z and A-Z.
.split("").join() doesn’t do anything. It changes it to an array and then back to a string.
split("").reverse().toString() would more conventionally be split("").reverse().join("")


#3

Hey thanks for the response!

I tried it without .join() and i get a type error

VM1453:3 Uncaught TypeError: str.toString(…).replace(…).toLowerCase(…).split(…).replace is not a function
at palindrome (:3:84)
at :16:1

And not only do i get an error if i remove it, the way i understand it is if i dont use .join() its left as an array and i cant compare it to the original string.

Im pretty new so i might be getting it wrong. Please explain.


#4

why do you have to replace 3 times?

What’s wrong with

var oldStr = str.toString().replace(/[^0-9a-zA-Z]/g, "").toLowerCase();
var newStr= oldStr.split("").reverse().join("");

return oldStr==newStr;

#5

I was just going step by step and i ran into so many roadblocks i just stopped when i got it to work and came here for help.

Nothing is wrong with your code, its much better than what i wrote. That is specifically why i am here asking. Thanks for the response.


#6

you need to add a separator “” to your join function, because by default it joins the array with “,” as a separator.

I think that’s what tripped your up.


#7

Cool! I understand what you are saying but i dont understand how to apply it correctly.
Without just using your provided (better) answer…
And since my current code works with and without the separator can you explain to me what i could omit from my code, or i how i would shorten it while including the separator.


#8

well, first thing you don’t need is toString(), because str is already a string.

Then you ran that string through replace, that took care of all the blanks and symbols, then you run it to through toLowerCase, by this point, you’ve already obtained oldStr, but then you split the string into a character array, then joined it again.

When you did it without the empty separator, you got back a string with commas, which is why you had to run replace again, when you did it with “”, replace() replaced nothing. So really everything after str.toString().replace(/[^0-9a-zA-Z]/g, '').toLowerCase() was just extra work that you don’t need to be doing, you were solving problems that weren’t the root of your problem.

Same thing with your new string, instead of using join(""), which returns a string you wanted, you used toString for arrays, which gives you array elements separated by commas, which forced you to use replace() to clean it up.


#9

Pretty sure you don’t need that .toString() method at all.

The psuedo code would go like

// Remove junk and set to lowercase
// Reverse string (split reverse join)
// Compare

You only need to use each method once.
IDK if this is a good hint for you but I can get more specific or show you my solution if you like.


#10

This.
DOH! i just realized they are passing everything in as a string already… i was accounting for regular numbers being used.

“We’ll also pass strings with special symbols, such as “2A33a2", “2A3 3a2”, and "2_A33#A2”.”


#12

Hey! I was going to reply to that questions. :angry:


#13

haha… sorry!! i brainfarted. I asked that question forgetting that its already a string.


#14

Well just in case you don’t know .split() creates an array, .reverse() works on the array NOT a string, and then .join() makes the string again from the array.


#15

maybe you could answer this

why newStr not reversed

var str = "1 EYE for of 1 eye."

 var newStr = str.replace(/[^0-9a-zA-Z]/g, '').toLowerCase().split().reverse().join("");

newStr

"1eyeforof1eye"

#16

Well for one .split() is missing the ""
It should be .split("")


#17

thats not for 1 thing, thats the whole thing. I think i need a few minutes break. My brain is mush. THANK YOU very much for the time you spent helping me.


#18

Yea can you show me your solution now please ?


#19

My Solution.


#20

sweet. thank you. I get it now.


#21

refactored

function palindrome(str) {
 
   var oldStr = str.replace(/[^0-9a-zA-Z]/g, '').toLowerCase();
  
  // Reverse string
  var newStr = oldStr.split("").reverse().join("");
  
  // Compare
  if (newStr === oldStr) {
    return true;
    
  } else {
    return false;
  }
}
palindrome("0_0 (: /-\ :) 0-0");