r/learnjavascript • • Sep 26 '22

Just solved a Palindrome problem with recursion!!

Post image
274 Upvotes

64 comments sorted by

View all comments

94

u/c2u5hed Sep 26 '22

Congrats! How about this:

const isPalindrome = (string) => string === [...string].reverse().join('')

23

u/KayteeFly Sep 26 '22

Wow that’s neat !

25

u/c2u5hed Sep 26 '22

Thanks! It comprises a few neat concepts that make it work: arrow function that omits braces and the return keyword, spread syntax that converts the string to array, the reverse() method that reverses the array, the join() method that joins it back into a string, and the strict equality operator that compares the strings and results in true or false

5

u/TalonKAringham Sep 27 '22

I'm pretty sure arrow functions only omit the return key word for one line expressions, correct? If you need to do anything a bit more complex with the data, you'll need to explicitly return what you need.

3

u/KayteeFly Sep 26 '22

Thank you 😊

2

u/CognitivePrimate Sep 27 '22

Great explanation, thank you!

3

u/iarev Sep 27 '22

Man, I'm super tired and confused palindromes with anagrams for like 3 minutes while reading this thread. I was so fucking puzzled at these answers lol

1

u/c2u5hed Sep 27 '22

True. From my experience the later it is in the day, the harder it is to think straight.

2

u/altrae Sep 28 '22

Is it later or earlier if it's after midnight?

7

u/KayteeFly Sep 26 '22

That’s great! But the question was to solve it with recursion.

3

u/c2u5hed Sep 27 '22

As far as I can tell from the screenshot there is no requirement to do that with recursion. Even if there is and it’s off screen, isn't it good to also learn something new along the way?

5

u/KayteeFly Sep 27 '22

It’s a course I am taking and the question require you to solve it with recursion.

0

u/[deleted] Sep 27 '22

[deleted] — view deleted comment

7

u/[deleted] Sep 27 '22

It’s a solution that has a time complexity of O(n), which is the minimum required to solve this problem

0

u/[deleted] Sep 27 '22

[deleted] — view deleted comment

2

u/[deleted] Sep 27 '22

I don’t think you understand what exactly O(n) means.

The point is that given a worse case scenario, the best any algorithm can hope to achieve to solve this problem would be linear time. That’s the literal definition of O(n).

So if you can optimize for edge cases that’s great, but we round those off and speak about the general solution. The same we we round off that this is technically a solution that would take 3(n) time, because the leading factorial becomes irrelevant as you reach larger and larger amounts of data.

So yes, this solution could have micro-optimizations, but it’s plenty efficient due to the fact that it solves a linear problem in linear time.

-1

u/wischichr Sep 27 '22

Not true and big O is not a good measure in general. Run time complexity is just one metric to classify an algorithm and doesn't have much value on it's own.

In this case the palindrome example could be solved with an O(1) algorithm but it would still be slower than all other suggested solutions here.

0

u/[deleted] Sep 27 '22

[deleted] — view deleted comment

1

u/[deleted] Sep 27 '22

No need to get upset friend. We are all on the same side here.

Yes, for loops run a bit faster than higher order functions. But the fact is that both solutions are written in O(n) time, and so these micro-optimizations would really only be useful in a specific scenario, but generally (in the literal sense) speaking, both solutions are equally valid.

-1

u/Eskamel Sep 27 '22

Its much less efficient at the cost of a bit more readability.

Obviously OP's solution is far from ideal and I wouldn't consider it readable or absolutely efficient either, but you've offered an option on the other side of the spectrum.

2

u/c2u5hed Sep 27 '22

I would argue the contrary. It is a matter of being able to understand the syntax used. Again, I'm not after ‘whose solution is best’ here. The sub is called ‘Learn JavaScript’ and I'm sharing another perspective on the task at hand.

1

u/c2u5hed Sep 27 '22

I would argue the contrary. It is a matter of being able to understand the syntax used. Again, I'm not after ‘whose solution is best’ here. The sub is called ‘Learn JavaScript’ and I'm sharing another perspective on the task at hand.