Skip to content

Commit bf9100f

Browse files
authored
Added CheckPalindrome (TheAlgorithms#213)
* Added CheckPalindrome contributed by @Swapnil-2001
1 parent 3e78762 commit bf9100f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

String/CheckPalindrome.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Palindrome check is case sensitive; i.e. Aba is not a palindrome
2+
// input is a string
3+
const checkPalindrome = (str) => {
4+
// check that input is a string
5+
if (typeof str !== 'string') {
6+
return 'Not a string'
7+
}
8+
// Store the length of the input string in a variable
9+
const length = str.length
10+
if (length === 0) {
11+
return 'Empty string'
12+
}
13+
// Iterate through the length of the string
14+
// Compare the first character to the last, the second character to the second last, and so on
15+
for (let i = 0; i < length / 2; i++) {
16+
// at the first instance of a mismatch
17+
if (str[i] !== str[length - 1 - i]) {
18+
return 'Not a Palindrome'
19+
}
20+
}
21+
return 'Palindrome'
22+
}
23+
24+
console.log(checkPalindrome('madam'))
25+
console.log(checkPalindrome('abcd'))

0 commit comments

Comments
 (0)