Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

test: added tests for Decimal Isolate function #1752

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

Open
HRIDYANSHU054 wants to merge 1 commit into TheAlgorithms:master
base: master
Choose a base branch
Loading
from HRIDYANSHU054:feat/DecimalIsolate/Tests
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
feat: added tests for Decimal Isolate function
  • Loading branch information
HRIDYANSHU054 committed Oct 25, 2024
commit 053800f3357a278b6c2c4970b049f1f444920d9c
11 changes: 8 additions & 3 deletions Maths/DecimalIsolate.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/*
* function isolates the decimal part of a number.
* Take the number and subtract it from the floored number.
* Return the result.
* This function isolates the decimal part of a number.
* - If input is a number or a numeric string, it isolates and returns the decimal part.
* - If input is an array:
* - It isolates the decimal part of the first element.
* - If the array contains more than one element, only the first element is considered.
* - If the array is empty, it returns 0.
* - If input is not a number, a numeric string, or a valid first element array,
* the function returns 0.
*/

export const decimalIsolate = (number) => {
Expand Down
44 changes: 44 additions & 0 deletions Maths/test/DecimalIsolate.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { decimalIsolate } from '../DecimalIsolate'

const invalidInputs = [
{ input: NaN, description: 'NaN' },
{ input: null, description: 'null' },
{ input: undefined, description: 'undefined' },
{ input: 'a string', description: 'a string' },
{ input: { a: 54.34 }, description: 'an object' },
{
input: ['OneDotTwoThree', 4.56, 7.89],
description: 'an array with invalid first element'
}
]

describe('DecimalIsolate', () => {
it('should isolate the decimal part of a positive number', () => {
expect(decimalIsolate(12.34)).toBe(0.34)
})

it('should isolate the decimal part of a negative number', () => {
expect(decimalIsolate(-456.789)).toBe(0.789)
})

it('should return 0 when the number is a whole number', () => {
expect(decimalIsolate(100)).toBe(0)
})

it('should isolate the decimal part of a number string', () => {
expect(decimalIsolate('12.34')).toBe(0.34)
})

it('should isolate the decimal part of the first element of an array if it is convertible to a number', () => {
expect(decimalIsolate([98.76, { a: 76.45 }])).toBe(0.76)
})

describe('Invalid Inputs', () => {
it.each(invalidInputs)(
'should return 0 for invalid input when input is $description',
({ input }) => {
expect(decimalIsolate(input)).toBe(0)
}
)
})
})

AltStyle によって変換されたページ (->オリジナル) /