diff --git a/src/algorithms/string/palindrome/README.md b/src/algorithms/string/palindrome/README.md new file mode 100644 index 0000000000..c9d774b05e --- /dev/null +++ b/src/algorithms/string/palindrome/README.md @@ -0,0 +1 @@ +This is a *WIP*. diff --git a/src/algorithms/string/palindrome/__true__/palindrome.test.js b/src/algorithms/string/palindrome/__true__/palindrome.test.js new file mode 100644 index 0000000000..d04a334a9c --- /dev/null +++ b/src/algorithms/string/palindrome/__true__/palindrome.test.js @@ -0,0 +1,8 @@ +import palindrome from '../palindrome'; + +describe('palindrome', () => { + it('should return true if word is a palindrome', () => { + expect(palindrome('racecar')).toBe(true); + expect(palindrome('car')).toBe(false); + }); +}); diff --git a/src/algorithms/string/palindrome/palindrome.js b/src/algorithms/string/palindrome/palindrome.js new file mode 100644 index 0000000000..9d0d9ea13f --- /dev/null +++ b/src/algorithms/string/palindrome/palindrome.js @@ -0,0 +1,8 @@ +/** + * @param {string} string + * @return {boolean} + */ +export default function palindrome(string) { + const reverseString = string.split('').reverse().join(''); + return reverseString === string; +}