File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ // JavaScript implementation of palindrome check
2+ //
3+ // Author: Shriharsha KL
4+
5+ /**
6+ * @description Check if the input is a palindrome
7+ *
8+ * @param {string|number } input
9+ * @returns {boolean } is input a palindrome?
10+ */
11+ function checkPalindrome ( input ) {
12+ // Only strings and numbers can be palindrome
13+ if ( typeof input !== 'string' && typeof input !== 'number' ) {
14+ return null ;
15+ }
16+
17+ // Convert given number to string
18+ if ( typeof input === 'number' ) {
19+ input = String ( input ) ;
20+ }
21+
22+ return input === input . split ( '' ) . reverse ( ) . join ( '' ) ;
23+ }
24+
25+ // Test
26+ let input = 'ABCDCBA' ;
27+ console . log ( checkPalindrome ( input ) ) ; // true
28+
29+ input = 12321 ;
30+ console . log ( checkPalindrome ( input ) ) ; // true
31+
32+ input = 123.321 ;
33+ console . log ( checkPalindrome ( input ) ) ; // true
34+
35+ input = 'ABCD' ;
36+ console . log ( checkPalindrome ( input ) ) ; // false
37+
38+ input = 123.4 ;
39+ console . log ( checkPalindrome ( input ) ) ; // false
40+
41+ input = { } ;
42+ console . log ( checkPalindrome ( input ) ) // null
You can’t perform that action at this time.
0 commit comments