EXERCISE · AlgoCasts
Palindrome
Given a string, return true if the string is a palindrome or false if it is not. Palindromes are strings that form the same word if it is reversed.
Directions
Directions
Given a string, return true if the string is a palindrome or false if it is not. Palindromes are strings that form the same word if it is reversed. *Do* include spaces and punctuation in determining if the string is a palindrome.
Examples#
JS
palindrome("abba") === truepalindrome("abcdefg") === falseTry it first#
Copy the stub, make the 8 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.
JSexercises/palindrome/index.js
function palindrome(str) {} module.exports = palindrome;Solution#
JScompleted_exercises/palindrome/index.js
function palindrome(str) { const reversed = str .split('') .reverse() .join(''); return str === reversed;} module.exports = palindrome;function palindrome(str) { const res = str.toLowerCase(); return res.split('').every((char, i) => { return char === res[res.length - i - 1]; });}console.log(palindrome('Racecar'))const palindrome = require('./index'); test('palindrome function is defined', () => { expect(typeof palindrome).toEqual('function');}); test('"aba" is a palindrome', () => { expect(palindrome('aba')).toBeTruthy();}); test('" aba" is not a palindrome', () => { expect(palindrome(' aba')).toBeFalsy();}); test('"aba " is not a palindrome', () => { expect(palindrome('aba ')).toBeFalsy();}); test('"greetings" is not a palindrome', () => { expect(palindrome('greetings')).toBeFalsy();}); test('"1000000001" a palindrome', () => { expect(palindrome('1000000001')).toBeTruthy();}); test('"Fish hsif" is not a palindrome', () => { expect(palindrome('Fish hsif')).toBeFalsy();}); test('"pennep" a palindrome', () => { expect(palindrome('pennep')).toBeTruthy();}); Verbatim from the repo at 30cf7634. Alternate tabs are the commented-out solutions with the leading // removed and nothing else changed. Run the tests locally with npx jest palindrome inside the fork.
See also