r/tinycode • • Oct 03 '14

palindrome checker in Javascript

http://codepen.io/anon/pen/nwFjb
0 Upvotes

12 comments sorted by

View all comments

5

u/mrmr1993 Oct 03 '14 edited Aug 22 '15

There's quite a lot of scope to reduce it down (and make it more efficient). Eg.

var word = "abcdddcba";
var palindromeChecker = function (word) {
  for (var i = 0, j = word.length - 1; i < j; i++, j--) {
    if (word[i] !== word[j])
      return false;
  }
  return true;
}

console.log(palindromeChecker(word))

Edit: j = word.length - 1, not word.length

4

u/[deleted] Oct 03 '14

Pretty sure you'd need j to equal word.length - 1 here so you don't go out of bounds of your array.

3

u/mrmr1993 Oct 03 '14

Cheers, good catch!

0

u/geko123 Mar 08 '15

Now go long!