Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e898704

Browse files
Palindrome
1 parent 1bf92f9 commit e898704

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎Palindrome/index.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Palindrome
2+
// Write a function that takes in a string that ignores special character and underscores, is case insensitive, ignores spaces, and test if it is a palindrome
3+
4+
const isPalindrome = str => {
5+
const forwardStr = str
6+
.toLowerCase()
7+
.split('')
8+
.filter(el => {
9+
return 'abcdefghijklmnopqrstuvwxyz'.includes(el)
10+
})
11+
.join('')
12+
const reverseStr = forwardStr
13+
.split('')
14+
.reverse()
15+
.join('')
16+
17+
return forwardStr === reverseStr
18+
}
19+
20+
console.log(isPalindrome('tacocat'))
21+
console.log(isPalindrome('A man, a plan, a canal. Panama'))
22+
console.log(isPalindrome('My age is 0, 0 si ega ym.'))
23+
console.log(isPalindrome('.0_0 (:/-:) 0-0'))
24+
console.log(isPalindrome('Not a palindrome'))
25+
26+
//
27+
//
28+
// or using a regular expression
29+
const isPalindromeRegex = str => {
30+
const forwardStr = str.toLowerCase().replace(/[\W_]/g, '')
31+
const reverseStr = forwardStr
32+
.split('')
33+
.reverse()
34+
.join('')
35+
36+
return forwardStr === reverseStr
37+
}
38+
39+
console.log(isPalindromeRegex('tacocat'))
40+
console.log(isPalindromeRegex('A man, a plan, a canal. Panama'))
41+
console.log(isPalindromeRegex('My age is 0, 0 si ega ym.'))
42+
console.log(isPalindromeRegex('.0_0 (:/-:) 0-0'))
43+
console.log(isPalindromeRegex('Not a palindrome'))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /