Skip to content

Commit 73f0fab

Browse files
committed
feat: add Strings section with checkPalindrome algorithm
1 parent 2d50357 commit 73f0fab

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Strings/checkPalindrome.js

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy