Skip to content

feat: add Strings section with checkPalindrome algorithm #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Strings/checkPalindrome.js
Original file line number Diff line number Diff line change
@@ -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