File tree 1 file changed +5
-12
lines changed
1 file changed +5
-12
lines changed Original file line number Diff line number Diff line change @@ -5,20 +5,13 @@ const checkPalindrome = (str) => {
5
5
if ( typeof str !== 'string' ) {
6
6
return 'Not a string'
7
7
}
8
- // Store the length of the input string in a variable
9
- const length = str . length
10
- if ( length === 0 ) {
8
+ if ( str . length === 0 ) {
11
9
return 'Empty string'
12
10
}
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'
11
+ // Reverse only works with array, thus conevert the string to array, reverse it and convert back to string
12
+ // return as palindrome if the reversed string is equal to the input string
13
+ const reversed = [ ...str ] . reverse ( ) . join ( '' )
14
+ return str === reversed ? 'Palindrome' : 'Not a Palindrome'
22
15
}
23
16
24
17
export { checkPalindrome }
You can’t perform that action at this time.
0 commit comments