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

Commit73f0fab

Browse files
committed
feat: add Strings section with checkPalindrome algorithm
1 parent2d50357 commit73f0fab

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+
functioncheckPalindrome(input){
10+
// Only strings and numbers can be palindrome
11+
if(typeofinput!=='string'&&typeofinput!=='number'){
12+
returnnull;
13+
}
14+
15+
// Convert given number to string
16+
if(typeofinput==='number'){
17+
input=String(input);
18+
}
19+
20+
returninput===input.split('').reverse().join('');
21+
}
22+
23+
// Test
24+
letinput='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)

[8]ページ先頭

©2009-2025 Movatter.jp