diff --git a/strings/README.md b/strings/README.md index 59917c49..8b37427b 100644 --- a/strings/README.md +++ b/strings/README.md @@ -7,3 +7,7 @@ ### C# Please use [.Net Finddle](https://dotnetfiddle.net/) 1. [Palindrome Check](csharp/palindrome.cs) + +### 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