From bf9100f40f3482b50686b0fbc5d6a99d5d512f8e Mon Sep 17 00:00:00 2001 From: Swapnil-2001 <53232360+Swapnil-2001@users.noreply.github.com> Date: Tue, 30 Jun 2020 09:34:33 +0530 Subject: [PATCH] Added CheckPalindrome (#213) * Added CheckPalindrome contributed by @Swapnil-2001 --- String/CheckPalindrome.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 String/CheckPalindrome.js diff --git a/String/CheckPalindrome.js b/String/CheckPalindrome.js new file mode 100644 index 000000000..b368eae89 --- /dev/null +++ b/String/CheckPalindrome.js @@ -0,0 +1,25 @@ +// Palindrome check is case sensitive; i.e. Aba is not a palindrome +// input is a string +const checkPalindrome = (str) => { + // check that input is a string + if (typeof str !== 'string') { + return 'Not a string' + } + // Store the length of the input string in a variable + const length = str.length + if (length === 0) { + return 'Empty string' + } + // Iterate through the length of the string + // Compare the first character to the last, the second character to the second last, and so on + for (let i = 0; i < length / 2; i++) { + // at the first instance of a mismatch + if (str[i] !== str[length - 1 - i]) { + return 'Not a Palindrome' + } + } + return 'Palindrome' +} + +console.log(checkPalindrome('madam')) +console.log(checkPalindrome('abcd'))