Last active
February 3, 2018 20:35
-
-
Save GreggSetzer/0b3ce91c9cd4a0a0e3ad224e5f815f32 to your computer and use it in GitHub Desktop.
Javascript Interview Question: Palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a function that checks if a string is a palindrome. | |
*/ | |
//Most direct solution | |
function palindrome(str) { | |
return str === str.split('').reverse().join(''); | |
} | |
//Solution that uses Array.every() helper. Note, this is inefficient as you do twice the work. | |
function palindrome(str) { | |
return str.split('').every((char, i) => char === str[str.length - i - 1]); | |
} | |
//Jest | |
test('It should return true when the input is a palindrome.', () => { | |
expect(palindrome('aba')).toBeTruthy(); | |
}); | |
test('It should return false when the input is not a palindrome.', () => { | |
expect(palindrome('abc')).toBeFalsy(); | |
}); | |
//Try it | |
console.log(palindrome('aba')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment