loading...

Javascript Algorithms #2: Palindromes

Obosi Philip on August 01, 2018

Palindromes! Palindromes!! Palindromes!!! Ooooh boy. I'm pretty sure that by now you are wondering what these are. You know personally as a soft... [Read Full]
markdown guide
 

Great explanation Justine, especially using the .each API. Since javascript is so versatile, I am adding one more solution which uses iterator from my previous article dev.to/kepta/how-i-learned-to-stop...

function isPalindrome(str) {
   const iter = [...str.toLowerCase()];
   for (let char of iter) {
      if (char !== iter.pop()) { return false}
   }
   return true
}

The good thing about this is that it iterates only over half of the string to check if it is palindrome.

 

Do you have an idea on how you can do permutation of palindrome??

 

those memories of solving algorithms at freecodecamp.org

const isPalindrome = str => str.toLowerCase() === str.toLowerCase().split().reverse().join() 

 

nice, somehow didn't work until i added the splitters for split() and join()

const isPalindrome = str => str.toLowerCase() === str.split('').reverse().join('')
 

thats why we needs unit tests 😅

 

Thanks for the save. I didn't even look at the syntax closely. It's still a pretty concise solution though

 
 
code of conduct - report abuse