Skip to content

Commit cb9cd55

Browse files
author
Hridyanshu7
committed
feat: tests added for decimalIsolate and improved comments for clarity
1 parent 5426682 commit cb9cd55

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Diff for: Maths/DecimalIsolate.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/*
2-
* function isolates the decimal part of a number.
3-
* Take the number and subtract it from the floored number.
4-
* Return the result.
2+
* This function isolates the decimal part of a number.
3+
* - If input is a number or a numeric string, it isolates and returns the decimal part.
4+
* - If input is an array:
5+
* - It isolates the decimal part of the first element.
6+
* - If the array contains more than one element, only the first element is considered.
7+
* - If the array is empty, it returns 0.
8+
* - If input is not a number, a numeric string, or a valid first element array,
9+
* the function returns 0.
510
*/
611

712
export const decimalIsolate = (number) => {

Diff for: Maths/test/DecimalIsolate.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe } from 'node:test'
2+
import { decimalIsolate } from '../decimalIsolate'
3+
4+
const invalidInputs = [
5+
{ input: NaN, description: 'NaN' },
6+
{ input: null, description: 'null' },
7+
{ input: undefined, description: 'undefined' },
8+
{ input: 'a string', description: 'a string' },
9+
{ input: { a: 54.34 }, description: 'an object' },
10+
{
11+
input: ['OneDotTwoThree', 4.56, 7.89],
12+
description: 'an array with invalid first element'
13+
}
14+
]
15+
16+
describe('DecimalIsolate', () => {
17+
it('should isolate the decimal part of a positive number', () => {
18+
expect(decimalIsolate(12.34)).toBe(0.34)
19+
})
20+
21+
it('should isolate the decimal part of a negative number', () => {
22+
expect(decimalIsolate(-456.789)).toBe(0.789)
23+
})
24+
25+
it('should return 0 when the number is a whole number', () => {
26+
expect(decimalIsolate(100)).toBe(0)
27+
})
28+
29+
it('should isolate the decimal part of a number string', () => {
30+
expect(decimalIsolate('12.34')).toBe(0.34)
31+
})
32+
33+
it('should isolate the decimal part of the first element of an array if it is convertible to a number', () => {
34+
expect(decimalIsolate([98.76, { a: 76.45 }])).toBe(0.76)
35+
})
36+
37+
describe('Invalid Inputs', () => {
38+
it.each(invalidInputs)(
39+
'should return 0 for invalid input when input is $description',
40+
({ input }) => {
41+
expect(decimalIsolate(input)).toBe(0)
42+
}
43+
)
44+
})
45+
})

0 commit comments

Comments
 (0)