forked from AllAlgorithms/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckPalindrome.js
More file actions
42 lines (33 loc) · 951 Bytes
/
checkPalindrome.js
File metadata and controls
42 lines (33 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// JavaScript implementation of palindrome check
//
// Author: Shriharsha KL
/**
* @description Check if the input is a palindrome
*
* @param {string|number} input
* @returns {boolean} is input a palindrome?
*/
function checkPalindrome(input) {
// Only strings and numbers can be palindrome
if (typeof input !== 'string' && typeof input !== 'number') {
return null;
}
// Convert given number to string
if (typeof input === 'number') {
input = String(input);
}
return input === input.split('').reverse().join('');
}
// Test
let input = 'ABCDCBA';
console.log(checkPalindrome(input)); // true
input = 12321;
console.log(checkPalindrome(input)); // true
input = 123.321;
console.log(checkPalindrome(input)); // true
input = 'ABCD';
console.log(checkPalindrome(input)); // false
input = 123.4;
console.log(checkPalindrome(input)); // false
input = {};
console.log(checkPalindrome(input)) // null