+ 3

Longest palindrome

Please help me how to write this code for longest palindrome. Example: Karlo is a tuna nut returns tuna nut I bought a Toyota returns a toyota

19th May 2021, 8:54 AM
Tifa
2 odpowiedzi
+ 6
Hey! If you tried and got stuck, here's the code: const longestPalindrome = (str) => { let arr = str .replace(/ /g,"") .toLowerCase() .split("") arr .forEach(elem=>{ if([...arr].join("") !== [...arr].reverse().join("")){ arr.splice(0,1) } }) return arr.join("") } longestPalindrome("I want a Toyota") Output: atoyota Cheers.
19th May 2021, 10:12 AM
Isaac Fernandes
Isaac Fernandes - avatar
+ 4
Thanks, i really appreciated this.
19th May 2021, 10:23 AM
Tifa