Skip to content

Commit 053800f

Browse files
author
Hridyanshu7
committed
feat: added tests for Decimal Isolate function
1 parent ff314a2 commit 053800f

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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) => {

Maths/test/DecimalIsolate.test.js

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

0 commit comments

Comments
 (0)