Skip to content

Instantly share code, notes, and snippets.

@rafd
Forked from pjama/palindrome.js
Last active May 17, 2024 18:37
Show Gist options
  • Save rafd/9aa03c2fcf311c404780729daf55d85b to your computer and use it in GitHub Desktop.
Save rafd/9aa03c2fcf311c404780729daf55d85b to your computer and use it in GitHub Desktop.
W1D2 - Debugging Incorrect Code
// FUNCTION IMPLEMENTATION (MULTIPLE BUGS)
const isPalindrome = function(str) {
const noSpaces = str.split(" ").join("");
const midIndex = Math.floor(noSpaces.length/2);
const lastIndex = noSpaces.length - 1;
for (let i = 0; i < midIndex; i++) {
if (str[i] !== str[lastIndex - i]) return false;
}
}
// Assertion Function
const assertPalindrome = function(word, expected) {
console.log(`Testing isPalindrome(\"${word}\"):`);
const actual = isPalindrome(word);
if (actual === expected) {
console.log("\x1b[32m%s\x1b[0m", `\tPASS ✅ function returned ${actual}\n`);
} else {
console.log("\x1b[31m%s\x1b[0m", `\tFAIL 🛑 function returned ${actual} (expected ${expected})\n`);
}
}
// TEST CODE
// These should all pass assertion, but they don't.
assertPalindrome('p', true);
assertPalindrome('racecar', true);
assertPalindrome('my gym', true);
assertPalindrome('foo', false);
assertPalindrome('fluff', false);
assertPalindrome('just some random words', false);
// Bonus / Stretch: Uncomment these tests and figure out why these are also failing
// assertPalindrome('Kayak', true);
// assertPalindrome('a santa at NASA', true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment