From 9e6c98643298bd4bf828ab2c8c0382e752fc513f Mon Sep 17 00:00:00 2001 From: Haider Ali <> Date: Sat, 28 Oct 2023 17:17:34 +0500 Subject: [PATCH] add palindrome for string data type --- javascript/Utility/isStringPalindrome.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 javascript/Utility/isStringPalindrome.js diff --git a/javascript/Utility/isStringPalindrome.js b/javascript/Utility/isStringPalindrome.js new file mode 100644 index 000000000..a00d7a66a --- /dev/null +++ b/javascript/Utility/isStringPalindrome.js @@ -0,0 +1,18 @@ +function isStringPalindrome(str) { + + // find the length of a string + const len = str.length; + + // loop through half of the string + for (let i = 0; i < len / 2; i++) { + // check if first and last string are not same + if (str[i] !== str[len - 1 - i]) { + return 'It is not a palindrome'; + } + } + + return 'It is a palindrome'; +} + +// isStringPalindrome("REVIVER") result: It is a palindrome +// isStringPalindrome("ABCD") result: It is not a palindrome