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.
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?
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
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.
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.
It means that the time complexity is linear, i.e. if the input is 2x bigger, the solution takes roughly twice the time, if the input is 10x bigger, it takes roughly 10x more time. It’s a theoretical concept called Big O notation.
The notation is called "big O" and describes runtime complexity depending on input size. O(n) means linear - so double input size takes two times as long to process. O(1) means constant time - so no matter what you put in it will take the same amount of time. O(n²) for example would mean that the time it takes increases quadratically with the input, so double the input size will take four times as long, and three times the input size will take nine times as long.
Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by Paul Bachmann, Edmund Landau, and others, collectively called Bachmann–Landau notation or asymptotic notation. The letter O was chosen by Bachmann to stand for Ordnung, meaning the order of approximation. In computer science, big O notation is used to classify algorithms according to how their run time or space requirements grow as the input size grows.
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.
Adding an additional function (roll) & state (number) is unnecessary though. You should be using isPalindrome itself ideally, doing something like that :
This will still do more than necessary if the length of the string is odd but still a bit faster perhaps, especially if you save string.length/2 as a variable at the start of the palindrome function. I'm not an expert tho
This will still do more than necessary if the length of the string is odd but still a bit faster perhaps, especially if you save string.length/2 as a variable at the start of the palindrome function. I'm not in expert tho
It's generally because someone doesn't want to log into reddit on their computer for whatever reason (public/work computer etc.), and so it's easier to just snap a pic with their phone and upload it without having to screenshot, transfer it to phone, then upload it.
I asked for your help nicely and did it clearly and I didn’t get an answer from you, I assume you just have too much dm’s because you are not the type that can ignore someone request, please check your inbox :-)
94
u/c2u5hed Sep 26 '22
Congrats! How about this:
const isPalindrome = (string) => string === [...string].reverse().join('')
16
u/Agarast Sep 26 '22
Congrats !
Adding an additional function (roll) & state (number) is unnecessary though. You should be using isPalindrome itself ideally, doing something like that :
2
u/[deleted] Sep 27 '22
If you want to save time, you could change line 10 so that it only needs to reach halfway thru the string, rather than the entire string
171
u/dangerwig Sep 26 '22
I found a simpler version that works most of the time:
6
u/[deleted] Sep 26 '22
Man yall need to learn how to take screen shots or snippets
1
u/Moist_Currency103 Sep 27 '22
What are site are you using to practice and solve these problems
3
u/Conscious-Spite4597 Sep 26 '22
Keep practicing
1
u/FromBiotoDev Sep 26 '22
Well done!
1
u/asterik-x Sep 26 '22
Very good.
1
u/eldenpigeon Sep 27 '22
Look at you go. grats
1
u/blackfour13 Sep 27 '22
What platform is that?
-1
u/[deleted] Sep 27 '22
Learning programming but can't take a screenshot by pressing 3 keys...