Skip to content

Commit 0924f1c

Browse files
authored
merge: Improved abs function (TheAlgorithms#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+
const validNumber = +num // converted to number, also can use - Number(num)
14+
15+
if (Number.isNaN(validNumber)) {
16+
throw new TypeError('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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy