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

Commit 00c5383

Browse files
✅ NEW: Palindrome Number Check String Problem solution
1 parent 8509ee0 commit 00c5383

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

‎README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ npm test
1111
## Array Problems Solution -
1212
1. [Two Sum](https://leetcode.com/problems/two-sum/) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/blob/main/two-sum/)
1313
1. [Remove duplicates from sorted array]() - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-duplicates-from-sorted-array)
14-
1. [Remove elements from array]() - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-element)
14+
1. [Remove elements from array](https://leetcode.com/problems/remove-element) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/remove-element)
15+
16+
## String Problems Solution -
17+
1. [Check Palindrome Number](https://leetcode.com/problems/palindrome-number/) - [Solution](https://github.com/ManiruzzamanAkash/LeetCode-In-JS/tree/main/palindrome-number)

‎palindrome-number/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Check if a number is palindrome or not.
3+
*
4+
* @param {number} x
5+
*
6+
* @return {boolean}
7+
*/
8+
var isPalindrome = function(x) {
9+
/**
10+
* Solution 1 : JavaScript way
11+
*/
12+
// const reverseNumber = x.toString().split("").reverse().join("");
13+
// return x == reverseNumber;
14+
15+
/**
16+
* Solution 2: Traditional Programming way by reversing
17+
*/
18+
let initialX = x,
19+
reverseNumber = 0;
20+
21+
// Reverse the number.
22+
while (x != 0) {
23+
reverseNumber = parseInt(reverseNumber * 10 + parseInt(x % 10));
24+
x = parseInt(x / 10);
25+
}
26+
27+
// Handle for negative numbers.
28+
if (initialX < 0) {
29+
return initialX === -1 * reverseNumber;
30+
}
31+
32+
return initialX === reverseNumber;
33+
};
34+
35+
module.exports = isPalindrome;

‎palindrome-number/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const isPalindrome = require("./index");
2+
3+
test("121 would result true", () => {
4+
expect(isPalindrome(121)).toStrictEqual(true);
5+
});
6+
7+
test("-121 would result false", () => {
8+
expect(isPalindrome(-121)).toStrictEqual(false);
9+
});
10+
11+
test("10 would result false", () => {
12+
expect(isPalindrome(10)).toStrictEqual(false);
13+
});
14+
15+
test("12121 would result false", () => {
16+
expect(isPalindrome(12121)).toStrictEqual(true);
17+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /