Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 5305731

Browse files
jdrichardstechKent C. Dodds
authored andcommitted
feat: Add function to see if string is palindrome (#96)
This function removes all non-alphanumeric characters and spaces and then checks to see if the string is a palindrome
1 parent 0c8605d commit 5305731

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/checkPalindrome.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default checkPalindrome
2+
3+
/**
4+
*Original Source: https://stackoverflow.com/questions/41739845/freecodecamp-checking-for-palindromes
5+
*This method will check to see if
6+
*after removing all non alphanumeric characters,
7+
*the string is a palindrome
8+
* @param {String} str - string to check for Palindrome
9+
* @return {Boolean} - should return true or false
10+
*/
11+
12+
function checkPalindrome(str) {
13+
const newstr = str.replace(/[\W_]/g, '').toLowerCase()
14+
15+
if (newstr === newstr.split('').reverse().join('')) {
16+
return true
17+
}
18+
return false
19+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import shallowEqual from './shallow-equal'
3131
import swapCase from './swap-case'
3232
import endsWith from './endsWith'
3333
import round from './round'
34+
import checkPalindrome from './checkPalindrome'
3435

3536
export {
3637
initArray,
@@ -66,4 +67,5 @@ export {
6667
swapCase,
6768
endsWith,
6869
round,
70+
checkPalindrome,
6971
}

test/checkPalindrome.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava'
2+
import {checkPalindrome} from '../src'
3+
4+
test('string is not a palindrome', t => {
5+
const object = 'hello world'
6+
const expected = false
7+
const actual = checkPalindrome(object)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test(' string is a palindrome', t => {
12+
const object = 'l/o1&&9_J$$J 9!1ol'
13+
const expected = true
14+
const actual = checkPalindrome(object)
15+
t.deepEqual(actual, expected)
16+
})

0 commit comments

Comments
 (0)