File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 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' ) )
You can’t perform that action at this time.
0 commit comments