Skip to content

Commit 7272d42

Browse files
feat: add number of digits algorithm
1 parent 5c39e87 commit 7272d42

2 files changed

Lines changed: 52 additions & 37 deletions

File tree

‎Maths/NumberOfDigits.js‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
/**
2-
*
3-
* Author: dephraiim
4-
* License: GPL-3.0 or later
5-
*
6-
* Returns the number of digits of a given integer
7-
*
2+
* @function numberOfDigits
3+
* @description Counts the number of digits in a given integer.
4+
* @param {number} n - An integer
5+
* @returns {number} - The number of digits in n
6+
* @see https://en.wikipedia.org/wiki/Numerical_digit
7+
* @example numberOfDigits(123) => 3
8+
* @example numberOfDigits(0) => 1
9+
* @example numberOfDigits(-9999) => 4
810
*/
11+
const numberOfDigits = (n) => {
12+
if (!Number.isInteger(n)) {
13+
throw new TypeError('Input must be an integer')
14+
}
15+
return Math.abs(n).toString().length
16+
}
917

10-
const numberOfDigit = (n) => Math.abs(n).toString().length
11-
12-
/**
13-
* Returns the number of digits of a given integer.
14-
*
15-
* @param {number} n - The integer for which to count digits.
16-
* @returns {number} The number of digits in the integer.
17-
* @see https://math.stackexchange.com/questions/2145480/how-does-the-logarithm-returns-the-number-of-digits-of-a-number
18-
* @author dev-madhurendra
19-
*/
20-
const numberOfDigitsUsingLog = (n) =>
21-
n === 0 ? 1 : Math.floor(Math.log10(Math.abs(n))) + 1
22-
23-
export { numberOfDigit, numberOfDigitsUsingLog }
18+
export { numberOfDigits }

‎Maths/test/NumberOfDigits.test.js‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
import { numberOfDigit, numberOfDigitsUsingLog } from '../NumberOfDigits'
1+
import { numberOfDigits } from '../NumberOfDigits.js'
22

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

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

12-
it.each([
13-
[0, 1],
14-
[123423232, 9],
15-
[-123423232, 9],
16-
[9999, 4]
17-
])(
18-
'should return the correct number of digits in an integer',
19-
(value, expected) => {
20-
expect(numberOfDigitsUsingLog(value)).toBe(expected)
21-
}
22-
)
12+
test('numberOfDigits(123) returns 3', () => {
13+
expect(numberOfDigits(123)).toBe(3)
14+
})
15+
16+
test('numberOfDigits(9999) returns 4', () => {
17+
expect(numberOfDigits(9999)).toBe(4)
18+
})
19+
20+
test('numberOfDigits(-9999) returns 4', () => {
21+
expect(numberOfDigits(-9999)).toBe(4)
22+
})
23+
24+
test('numberOfDigits(1000000) returns 7', () => {
25+
expect(numberOfDigits(1000000)).toBe(7)
26+
})
27+
28+
test('numberOfDigits(-1) returns 1', () => {
29+
expect(numberOfDigits(-1)).toBe(1)
30+
})
31+
32+
test('throws TypeError for float input', () => {
33+
expect(() => numberOfDigits(1.5)).toThrow(TypeError)
34+
})
35+
36+
test('throws TypeError for string input', () => {
37+
expect(() => numberOfDigits('123')).toThrow(TypeError)
38+
})
39+
40+
test('throws TypeError for NaN', () => {
41+
expect(() => numberOfDigits(NaN)).toThrow(TypeError)
42+
})
2343
})

0 commit comments

Comments
 (0)