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
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: