Skip to content

Instantly share code, notes, and snippets.

@GreggSetzer
Last active February 3, 2018 20:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreggSetzer/0b3ce91c9cd4a0a0e3ad224e5f815f32 to your computer and use it in GitHub Desktop.
Save GreggSetzer/0b3ce91c9cd4a0a0e3ad224e5f815f32 to your computer and use it in GitHub Desktop.
Javascript Interview Question: Palindrome
/*
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