r/learnjavascript • u/KayteeFly • Sep 26 '22
Just solved a Palindrome problem with recursion!!
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
5
1
2
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
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
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) => false36
2
1
0
6
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
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
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
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
1
3
1
1
1
1
-1
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
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
94
u/c2u5hed Sep 26 '22
Congrats! How about this:
const isPalindrome = (string) => string === [...string].reverse().join('')