-
Notifications
You must be signed in to change notification settings - Fork 11
Palindrome exercise solution. #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
/* | ||
str is converted to array, array is reversed and converted back to string and checked if the result is equal to str. | ||
*/ | ||
if((str.split("").reverse("").join("")) == str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well the solution works.
Can you think of a way to take out this if
else
statement?
if((str.split("").reverse("").join("")) == str) { | |
if((str.split("").reverse("").join("")) == str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Thanks for the feedback.
/* | ||
str is converted to array, array is reversed and converted back to string and checked if the result is equal to str. | ||
*/ | ||
if((str.split("").reverse("").join("")) == str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also you should always try to do both type and value comparisons using ===
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay.
@@ -0,0 +1,15 @@ | |||
//defines function reverseString and gives it a parameter str | |||
function reverseString(str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also your solution will fail for a case such as Racecar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Racecar is not a palindrome because the reverse gives racecaR. @DanCarl857 please I don't understand why a case like that should be considered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically it's a palindrome because if you make it all lowercase you get a palindrome.
So try making the entire string all lowercase or all uppercase before checking.
PS: Don't forget the mock interview session in a 2mins. Link on slack
No description provided.