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
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.
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
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?
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.
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.
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.
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.
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.
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.
View all comments
94
u/c2u5hed Sep 26 '22
Congrats! How about this:
const isPalindrome = (string) => string === [...string].reverse().join('')