r/learnjavascript • • Sep 26 '22

Just solved a Palindrome problem with recursion!!

Post image
274 Upvotes

64 comments sorted by

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

3

u/KayteeFly Sep 26 '22

Thank you 😊

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.

2

u/CognitivePrimate Sep 27 '22

Great explanation, thank you!

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.

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?

-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.

0

u/[deleted] Sep 27 '22

[deleted]

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]

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/Jamstan_ Sep 27 '22

What is O(n)

2

u/kap89 Sep 27 '22

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.

2

u/wischichr Sep 27 '22

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.

https://en.wikipedia.org/wiki/Big_O_notation

2

u/WikiSummarizerBot Sep 27 '22

Big O notation

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.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

-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]

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.

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 :

const isPalindrome = (str) => { 
    if(str.length < 2) return true 
    if( str[0] === str[str.length-1]) return isPalindrome(str.slice(1,-1)) 
    return false 
}

8

u/oze4 Sep 26 '22

Is "a" a palindrome?

9

u/fusion407 Sep 27 '22

Am "I" a palindrome?

2

u/oze4 Sep 27 '22

Damn that's solid.

2

u/connurp Sep 27 '22

dood…

5

u/Agarast Sep 26 '22

Yes

0

u/ahref Sep 26 '22

What about ""

6

u/[deleted] Sep 27 '22

It reads the same both forwards and backwards, so yes

1

u/KayteeFly Sep 26 '22

Thank you

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

1

u/KayteeFly Sep 27 '22

Teach me please !

2

u/[deleted] Sep 27 '22 edited Sep 27 '22

if (number >=string.length/2)

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

EDIT: I'm dumb. This will be better:

if (number>=(string.length-1)/2)

1

u/[deleted] Sep 27 '22

if (number >=string.length/2)

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

171

u/dangerwig Sep 26 '22

I found a simpler version that works most of the time:

const isPalindrome = (string) => false

36

u/[deleted] Sep 26 '22

Choked on my coffee laughing thank you +1

2

u/__mystersir Sep 27 '22

The showing of the “false” on a palindrome is actually a feature

1

u/fusion407 Sep 27 '22

This is the way

0

u/[deleted] Sep 26 '22

[deleted]

4

u/bryku helpful Sep 27 '22

As all good thongs should be

1

u/fujiman Sep 27 '22

What kind of pos thong doesn't follow the 80/20 rule?

6

u/[deleted] Sep 26 '22

Man yall need to learn how to take screen shots or snippets

-27

u/KayteeFly Sep 26 '22

When you make comment like these. Do you consider the circumstances people are in or it’s just what you think.

13

u/enbacode Sep 26 '22

In what circumstances in this funky little world would you prefer an actual photograph over a screenshot

5

u/[deleted] Sep 26 '22

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.

6

u/iarev Sep 27 '22

So enough energy to ask strangers for help, but too lazy to make it presentable for them.

0

u/[deleted] Sep 28 '22

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 :-)

2

u/iarev Sep 28 '22

Paste the code into a code block so people can check it in their own consoles.

1

u/[deleted] Sep 29 '22

I’m not OP, check your Inbox 🇨🇭

1

u/Rus_s13 Sep 26 '22

Might be in a library, testing centre or other public setting without the functionality to post from, or transfer/email the photo to themself to send.

2

u/[deleted] Sep 26 '22

Lol ok

1

u/Moist_Currency103 Sep 27 '22

What are site are you using to practice and solve these problems

1

u/KayteeFly Sep 27 '22

I bought colt Steele’s algorithms and data structures Masterclass on udemy

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...

0

u/KayteeFly Sep 27 '22

Are you 12 ? Lol 😂

-1

u/[deleted] Sep 27 '22

Even a toddler would know better and spare our eyes by taking a simple screenshot.

Since you're on Windows, here: https://www.wikihow.com/Take-a-Screenshot-in-Microsoft-Windows

0

u/KayteeFly Sep 27 '22

Don’t look at it then 😂

0

u/[deleted] Sep 27 '22

You're calling me a 12 yo with that kind of toddler repartee? Sheesh