Merge pull request #15 from paulsonjpaul/main

feat(strings): Add Palindrome Checker for JavaScript
pull/16/head
Ming Tsai 2021-01-20 07:58:09 -04:00 committed by GitHub
commit 23b75e89ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -3,3 +3,7 @@
### C or C++
1. [Palindrome Check](c-or-cpp/palindrome.c)
### JavaScript
1. [Palindrome Check](js/palindrome.js)

View File

@ -0,0 +1,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 alpha-numeric word
let properStr = str
.replace(/[_\W]/g, "")
.toLowerCase();
// Now reverse the proper string
let 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"));