+ 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
2 Réponses
+ 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.
+ 4
Thanks, i really appreciated this.