-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(maths): module to allows users to perform mathematical operations
- Loading branch information
1 parent
384d020
commit 126da2e
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Maths | ||
|
||
## Description | ||
|
||
This feature allows you to perform mathematical operations such as addition, subtraction, multiplication, and division. And we will work on more operations in the future. | ||
|
||
## Usage | ||
|
||
```javascript | ||
import { sum, subtract, multiplication , divide } 'world-clockify'; | ||
const sum = sum(1, 2, 3, 4); | ||
const difference = subtract(10, 5); | ||
const product = multiplication (2, 3, 4); | ||
const quotient = divide(20, 2, 5); | ||
``` | ||
|
||
## Expected Output | ||
|
||
```bash | ||
10 | ||
5 | ||
24 | ||
``` | ||
|
||
## Arguments | ||
|
||
Every function can take multiple arguments. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './date/date.js'; | ||
export * from './productivity/productivity.js'; | ||
export * from './maths/basic.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Note: All function will accept unlimited number of arguments. | ||
* Return sum of numbers | ||
*/ | ||
|
||
export const sum = (...args: any[]): number => { | ||
if (args.length === 0) return 0; | ||
return args.reduce((acc, num) => acc + num, 0); | ||
}; | ||
|
||
/** | ||
* Return substraction | ||
*/ | ||
|
||
export const subtract = (...args: number[]): number => { | ||
if (args.length === 0) return 0; | ||
const [first = 0, ...rest] = args; // Default to 0 if args is empty | ||
|
||
return rest.reduce((acc, num) => acc - num, first); | ||
}; | ||
|
||
/** | ||
* Return multiplication | ||
*/ | ||
|
||
export const multiplication = (...args: number[]): number => { | ||
if (args.length === 0) return 0; | ||
if (args.length === 0) return 1; | ||
return args.reduce((acc, num) => acc * num, 1); | ||
}; | ||
|
||
/** | ||
* Return division | ||
*/ | ||
|
||
export const divide = (...args: number[]): number => { | ||
const [first = 1, ...rest] = args; // Start with the first element as initial value | ||
return rest.reduce((acc, num) => (num !== 0 ? acc / num : acc), first); | ||
}; | ||
|
||
// TODO : Add more math operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { sum, subtract, multiplication, divide } from '../src/maths/basic.js'; | ||
|
||
describe('Math module helper', () => { | ||
it('should pass sum', () => { | ||
let a = 10; | ||
let b = 20; | ||
let c = 30; | ||
expect(sum(a, b, c)); | ||
}); | ||
it('should pass substract', () => { | ||
let a = 10; | ||
let b = 20; | ||
let c = 30; | ||
expect(subtract(a, b, c)); | ||
}); | ||
it('should pass multiplication', () => { | ||
let a = 10; | ||
let b = 20; | ||
let c = 30; | ||
expect(multiplication(a, b, c)); | ||
}); | ||
it('should pass divison', () => { | ||
let a = 10; | ||
let b = 20; | ||
expect(divide(a, b)); | ||
}); | ||
}); |