From 7584214c69268de277c0f0ea43348455a8b62642 Mon Sep 17 00:00:00 2001 From: Paulson J Paul Date: Wed, 20 Jan 2021 12:18:46 +0530 Subject: [PATCH] feat(strings): Add Palindrome Checker for JavaScript --- strings/README.md | 4 ++++ strings/js/palindrome.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 strings/js/palindrome.js diff --git a/strings/README.md b/strings/README.md index 362cc48a..e7637b20 100644 --- a/strings/README.md +++ b/strings/README.md @@ -3,3 +3,7 @@ ### C or C++ 1. [Palindrome Check](c-or-cpp/palindrome.c) + +### JavaScript + +1. [Palindrome Check](js/palindrome.js) diff --git a/strings/js/palindrome.js b/strings/js/palindrome.js new file mode 100644 index 00000000..37516755 --- /dev/null +++ b/strings/js/palindrome.js @@ -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")); \ No newline at end of file