Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Changes fromall 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 deletionsStrings/checkPalindrome.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp