Skip to content

algorithm: check if integer is palindrome #1177

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

Merged
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions Maths/isPalindromeIntegerNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @function isPalindromeIntegerNumber
* @param { Number } x
* @returns {boolean} - input integer is palindrome or not
*
* time complexity : O(log_10(N))
* space complexity : O(1)
*/
export function isPalindromeIntegerNumber (x) {
if (typeof x !== 'number') {
throw new TypeError('Input must be a integer number')
}
// check x is integer
if (!Number.isInteger(x)) {
return false
}

// if it has '-' it cannot be palindrome
if (x < 0) return false

// make x reverse
let reversed = 0
let num = x

while (num > 0) {
const lastDigit = num % 10
reversed = reversed * 10 + lastDigit
num = Math.floor(num / 10)
}

// compare origin x and reversed are same
return x === reversed
}
32 changes: 32 additions & 0 deletions Maths/test/isPalindromeIntegerNumber.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isPalindromeIntegerNumber } from '../isPalindromeIntegerNumber'

describe('isPalindromeIntegerNumber', () => {
it('expects to return true when length of input is 1', () => {
expect(isPalindromeIntegerNumber(6)).toEqual(true)
})

it('expects to return true when input is palindrome', () => {
expect(isPalindromeIntegerNumber(121)).toEqual(true)
expect(isPalindromeIntegerNumber(12321)).toEqual(true)
expect(isPalindromeIntegerNumber(1221)).toEqual(true)
})

it('expects to return false when input is not palindrome', () => {
expect(isPalindromeIntegerNumber(189)).toEqual(false)
})

it('expects to return false when input is minus', () => {
expect(isPalindromeIntegerNumber(-121)).toEqual(false)
expect(isPalindromeIntegerNumber(-12321)).toEqual(false)
})

it('expects to return false when input is not integer number', () => {
expect(isPalindromeIntegerNumber(123.456)).toEqual(false)
})

it('expects to throw error when input is not a number', () => {
expect(() => isPalindromeIntegerNumber(undefined)).toThrowError()
expect(() => isPalindromeIntegerNumber({ key: 'val' })).toThrowError()
expect(() => isPalindromeIntegerNumber([])).toThrowError()
})
})
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