Skip to content

Commit 45f0b7c

Browse files
authored
algorithm: check if integer is palindrome (#1177)
* feat: Add check number case * style: Fix formatting * fix: Remove number check part * feat: Create isPalindromeIntegerNumber * test: Add minus number case
1 parent 148ebd6 commit 45f0b7c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Maths/isPalindromeIntegerNumber.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @function isPalindromeIntegerNumber
3+
* @param { Number } x
4+
* @returns {boolean} - input integer is palindrome or not
5+
*
6+
* time complexity : O(log_10(N))
7+
* space complexity : O(1)
8+
*/
9+
export function isPalindromeIntegerNumber (x) {
10+
if (typeof x !== 'number') {
11+
throw new TypeError('Input must be a integer number')
12+
}
13+
// check x is integer
14+
if (!Number.isInteger(x)) {
15+
return false
16+
}
17+
18+
// if it has '-' it cannot be palindrome
19+
if (x < 0) return false
20+
21+
// make x reverse
22+
let reversed = 0
23+
let num = x
24+
25+
while (num > 0) {
26+
const lastDigit = num % 10
27+
reversed = reversed * 10 + lastDigit
28+
num = Math.floor(num / 10)
29+
}
30+
31+
// compare origin x and reversed are same
32+
return x === reversed
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { isPalindromeIntegerNumber } from '../isPalindromeIntegerNumber'
2+
3+
describe('isPalindromeIntegerNumber', () => {
4+
it('expects to return true when length of input is 1', () => {
5+
expect(isPalindromeIntegerNumber(6)).toEqual(true)
6+
})
7+
8+
it('expects to return true when input is palindrome', () => {
9+
expect(isPalindromeIntegerNumber(121)).toEqual(true)
10+
expect(isPalindromeIntegerNumber(12321)).toEqual(true)
11+
expect(isPalindromeIntegerNumber(1221)).toEqual(true)
12+
})
13+
14+
it('expects to return false when input is not palindrome', () => {
15+
expect(isPalindromeIntegerNumber(189)).toEqual(false)
16+
})
17+
18+
it('expects to return false when input is minus', () => {
19+
expect(isPalindromeIntegerNumber(-121)).toEqual(false)
20+
expect(isPalindromeIntegerNumber(-12321)).toEqual(false)
21+
})
22+
23+
it('expects to return false when input is not integer number', () => {
24+
expect(isPalindromeIntegerNumber(123.456)).toEqual(false)
25+
})
26+
27+
it('expects to throw error when input is not a number', () => {
28+
expect(() => isPalindromeIntegerNumber(undefined)).toThrowError()
29+
expect(() => isPalindromeIntegerNumber({ key: 'val' })).toThrowError()
30+
expect(() => isPalindromeIntegerNumber([])).toThrowError()
31+
})
32+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy