Skip to content

Commit 708e4d4

Browse files
authored
Merge pull request AllAlgorithms#36 from klshriharsha/master
Add checkPalindrome.js
2 parents 978fa01 + c67793a commit 708e4d4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

strings/checkPalindrome.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

0 commit comments

Comments
 (0)