gilles-margerin / fcc-jscertification-projects

0 stars 0 forks source link

palyndrome-checker.js #2

Open samwho opened 4 years ago

samwho commented 4 years ago

Again, looks correct to me.

You could make a small improvement and exit early as soon as you know it's not a palindrome, instead of doing a full check every time. How might you do it?

gilles-margerin commented 4 years ago

I've updated the code this way. I've also thought about this soluttion:

function palindrome(str) {
  const palCheck = str.toLowerCase().replace(/\W|_/g, "").split("");
  const test = palCheck.slice().reverse()
  return palCheck.join("") === test.join("") 
  }

Which would be better and why?

I've started reading that using for/while loops should be avoided and arrays or strings methods is preferred overall

samwho commented 4 years ago

I believe your new version that you just replied with will exit early if it’s obvious the strings don’t match. If you do “abc” == “123”, it will check the first two values “a” and “1” and realise they’re not the same and not bother checking the rest of the string.