forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.js
More file actions
22 lines (18 loc) · 708 Bytes
/
Copy pathpalindrome.js
File metadata and controls
22 lines (18 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// JavaScript Palindrome Checker: Checks whether a word is the same in reverse.
// Ignores punctuation, capitalization & spaces.
function isPalindrome(str) {
// First convert the string into proper alphanumeric word
const properStr = str.replace(/[_\W]/g, '').toLowerCase();
// Now reverse the proper string
const reverseStr = properStr.split('').reverse().join('');
/*
* Finally compare the proper string and reverse string and
* return true or false
*/
return properStr === reverseStr;
}
// Output to the console
console.log(isPalindrome('eye'));
console.log(isPalindrome('Mr. Owl ate my metal worm'));
console.log(isPalindrome('RAce C*_aR'));
console.log(isPalindrome('asdfggfrd'));