Files
JavaScript/String/CheckPalindrome.js
Alexandre Xavier c5fc353c32 Added tests for Strings algorithms (#390)
* test: added tests for check anagram function
2020-10-04 23:08:48 +05:30

25 lines
720 B
JavaScript

// 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'
}
export { checkPalindrome }