From 73f0fab7ab8b0be9fcc57d6142fbba27b6e87a79 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 20 Oct 2018 13:01:41 +0300 Subject: [PATCH] feat: add Strings section with checkPalindrome algorithm --- Strings/checkPalindrome.js | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Strings/checkPalindrome.js diff --git a/Strings/checkPalindrome.js b/Strings/checkPalindrome.js new file mode 100644 index 0000000000..d42c7544cb --- /dev/null +++ b/Strings/checkPalindrome.js @@ -0,0 +1,40 @@ +// JavaScript implementation of palindrome check +// More details: https://medium.freecodecamp.org/two-ways-to-check-for-palindromes-in-javascript-64fea8191fd7 +/** + * @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 \ No newline at end of file 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