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

Commit 0924f1c

Browse files
merge: Improved abs function (#923)
* feat: added validation, test codes * chore: remove useless words
1 parent 80cea08 commit 0924f1c

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

‎Maths/Abs.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/**
2-
* @function absVal
2+
* @function abs
33
* @description This script will find the absolute value of a number.
4-
* @param {Integer} num - The input integer
5-
* @return {Integer} - Absolute number of num.
6-
* @see [Absolute_value](https://en.wikipedia.org/wiki/Absolute_value)
7-
* @example absVal(-10) = 10
8-
* @example absVal(50) = 50
9-
* @example absVal(0) = 0
4+
* @param {number} num - The input integer
5+
* @return {number} - Absolute number of num.
6+
* @see https://en.wikipedia.org/wiki/Absolute_value
7+
* @example abs(-10) = 10
8+
* @example abs(50) = 50
9+
* @example abs(0) = 0
1010
*/
1111

12-
const absVal = (num) => {
13-
// Find absolute value of `num`.
14-
'use strict'
15-
if (num<0) {
16-
return-num
12+
const abs = (num) => {
13+
constvalidNumber=+num// converted to number, also can use - Number(num)
14+
15+
if (Number.isNaN(validNumber)) {
16+
thrownewTypeError('Argument is NaN - Not a Number')
1717
}
18-
// Executes if condition is not met.
19-
return num
18+
19+
return validNumber<0 ? -validNumber : validNumber// if number is less then zero mean negative then it converted to positive. i.e -> n = -2 = -(-2) = 2
2020
}
2121

22-
export { absVal }
22+
export { abs }

‎Maths/test/Abs.test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
import { absVal } from '../Abs'
1+
import { abs } from '../Abs'
2+
3+
describe('Testing abs function', () => {
4+
it('Testing for invalid types', () => {
5+
expect(() => abs('234a')).toThrow()
6+
expect(() => abs({})).toThrow()
7+
expect(() => abs([12, -32, -60])).toThrow()
8+
})
9+
10+
it('Testing for number of string type', () => {
11+
expect(abs('-345')).toBe(345)
12+
expect(abs('-345.455645')).toBe(345.455645)
13+
})
14+
15+
it('Testing for a boolean type', () => {
16+
expect(abs(true)).toBe(1)
17+
expect(abs(false)).toBe(0)
18+
})
219

3-
describe('absVal', () => {
420
it('should return an absolute value of a negative number', () => {
5-
const absOfNegativeNumber = absVal(-34)
21+
const absOfNegativeNumber = abs(-34)
622
expect(absOfNegativeNumber).toBe(34)
723
})
824

925
it('should return an absolute value of a positive number', () => {
10-
const absOfPositiveNumber = absVal(50)
26+
const absOfPositiveNumber = abs(50)
1127
expect(absOfPositiveNumber).toBe(50)
1228
})
1329

1430
it('should return an absolute value of a zero number', () => {
15-
const absOfPositiveNumber = absVal(0)
31+
const absOfPositiveNumber = abs(0)
1632
expect(absOfPositiveNumber).toBe(0)
1733
})
34+
35+
it('should return an absolute value of any floating number', () => {
36+
const absOfPositiveNumber = abs(-20.2034)
37+
expect(absOfPositiveNumber).toBe(20.2034)
38+
})
1839
})

0 commit comments

Comments
(0)

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