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