Skip to content

Instantly share code, notes, and snippets.

@potatowave
Forked from DaneSirois/palindrome.js
Created October 5, 2016 00:20
Show Gist options
  • Save potatowave/9f07c70d75cb2d743b81d0ca72150627 to your computer and use it in GitHub Desktop.
Save potatowave/9f07c70d75cb2d743b81d0ca72150627 to your computer and use it in GitHub Desktop.
W1D2 - Debugging Incorrect Code
function isPalindrome(str) {
str = str.split(" ").join("").toLowerCase();
var mid = Math.floor(str.length / 2);
var last = str.length - 1;
for (var i = 0; i <= mid; i++) {
// console.log(">>>>" + str[i] + " " + str[last - i]);
if (str[i] !== str[last - i]) {
return false;
}
}
return true;//never found unequal chars
}
// Test driver code. These should all log true.
console.log(isPalindrome('p') === true);
console.log(isPalindrome('foo') === false);
console.log(isPalindrome('racecar') === true);
console.log(isPalindrome('Kayak') === true);
console.log(isPalindrome('a santa at NASA') === true);
console.log(isPalindrome('live without evil') === false);
console.log(isPalindrome('just some random words') === false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment