Skip to content
Open
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
35 changes: 15 additions & 20 deletions Maths/NumberOfDigits.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
/**
*
* Author: dephraiim
* License: GPL-3.0 or later
*
* Returns the number of digits of a given integer
*
* @function numberOfDigits
* @description Counts the number of digits in a given integer.
* @param {number} n - An integer
* @returns {number} - The number of digits in n
* @see https://en.wikipedia.org/wiki/Numerical_digit
* @example numberOfDigits(123) => 3
* @example numberOfDigits(0) => 1
* @example numberOfDigits(-9999) => 4
*/
const numberOfDigits = (n) => {
if (!Number.isInteger(n)) {
throw new TypeError('Input must be an integer')
}
return Math.abs(n).toString().length
}

const numberOfDigit = (n) => Math.abs(n).toString().length

/**
* Returns the number of digits of a given integer.
*
* @param {number} n - The integer for which to count digits.
* @returns {number} The number of digits in the integer.
* @see https://math.stackexchange.com/questions/2145480/how-does-the-logarithm-returns-the-number-of-digits-of-a-number
* @author dev-madhurendra
*/
const numberOfDigitsUsingLog = (n) =>
n === 0 ? 1 : Math.floor(Math.log10(Math.abs(n))) + 1

export { numberOfDigit, numberOfDigitsUsingLog }
export { numberOfDigits }
54 changes: 37 additions & 17 deletions Maths/test/NumberOfDigits.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
import { numberOfDigit, numberOfDigitsUsingLog } from '../NumberOfDigits'
import { numberOfDigits } from '../NumberOfDigits.js'

describe('NumberOfDigits', () => {
it('should return the correct number of digits for an integer', () => {
expect(numberOfDigit(1234000)).toBe(7)
describe('numberOfDigits', () => {
test('numberOfDigits(0) returns 1', () => {
expect(numberOfDigits(0)).toBe(1)
})

it('should return the correct number of digits for a negative number', () => {
expect(numberOfDigit(-2346243)).toBe(7)
test('numberOfDigits(1) returns 1', () => {
expect(numberOfDigits(1)).toBe(1)
})

it.each([
[0, 1],
[123423232, 9],
[-123423232, 9],
[9999, 4]
])(
'should return the correct number of digits in an integer',
(value, expected) => {
expect(numberOfDigitsUsingLog(value)).toBe(expected)
}
)
test('numberOfDigits(123) returns 3', () => {
expect(numberOfDigits(123)).toBe(3)
})

test('numberOfDigits(9999) returns 4', () => {
expect(numberOfDigits(9999)).toBe(4)
})

test('numberOfDigits(-9999) returns 4', () => {
expect(numberOfDigits(-9999)).toBe(4)
})

test('numberOfDigits(1000000) returns 7', () => {
expect(numberOfDigits(1000000)).toBe(7)
})

test('numberOfDigits(-1) returns 1', () => {
expect(numberOfDigits(-1)).toBe(1)
})

test('throws TypeError for float input', () => {
expect(() => numberOfDigits(1.5)).toThrow(TypeError)
})

test('throws TypeError for string input', () => {
expect(() => numberOfDigits('123')).toThrow(TypeError)
})

test('throws TypeError for NaN', () => {
expect(() => numberOfDigits(NaN)).toThrow(TypeError)
})
})
Loading