mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00
merge: Improved abs
function (#923)
* feat: added validation, test codes * chore: remove useless words
This commit is contained in:
30
Maths/Abs.js
30
Maths/Abs.js
@ -1,22 +1,22 @@
|
||||
/**
|
||||
* @function absVal
|
||||
* @function abs
|
||||
* @description This script will find the absolute value of a number.
|
||||
* @param {Integer} num - The input integer
|
||||
* @return {Integer} - Absolute number of num.
|
||||
* @see [Absolute_value](https://en.wikipedia.org/wiki/Absolute_value)
|
||||
* @example absVal(-10) = 10
|
||||
* @example absVal(50) = 50
|
||||
* @example absVal(0) = 0
|
||||
* @param {number} num - The input integer
|
||||
* @return {number} - Absolute number of num.
|
||||
* @see https://en.wikipedia.org/wiki/Absolute_value
|
||||
* @example abs(-10) = 10
|
||||
* @example abs(50) = 50
|
||||
* @example abs(0) = 0
|
||||
*/
|
||||
|
||||
const absVal = (num) => {
|
||||
// Find absolute value of `num`.
|
||||
'use strict'
|
||||
if (num < 0) {
|
||||
return -num
|
||||
const abs = (num) => {
|
||||
const validNumber = +num // converted to number, also can use - Number(num)
|
||||
|
||||
if (Number.isNaN(validNumber)) {
|
||||
throw new TypeError('Argument is NaN - Not a Number')
|
||||
}
|
||||
// Executes if condition is not met.
|
||||
return num
|
||||
|
||||
return validNumber < 0 ? -validNumber : validNumber // if number is less then zero mean negative then it converted to positive. i.e -> n = -2 = -(-2) = 2
|
||||
}
|
||||
|
||||
export { absVal }
|
||||
export { abs }
|
||||
|
@ -1,18 +1,39 @@
|
||||
import { absVal } from '../Abs'
|
||||
import { abs } from '../Abs'
|
||||
|
||||
describe('Testing abs function', () => {
|
||||
it('Testing for invalid types', () => {
|
||||
expect(() => abs('234a')).toThrow()
|
||||
expect(() => abs({})).toThrow()
|
||||
expect(() => abs([12, -32, -60])).toThrow()
|
||||
})
|
||||
|
||||
it('Testing for number of string type', () => {
|
||||
expect(abs('-345')).toBe(345)
|
||||
expect(abs('-345.455645')).toBe(345.455645)
|
||||
})
|
||||
|
||||
it('Testing for a boolean type', () => {
|
||||
expect(abs(true)).toBe(1)
|
||||
expect(abs(false)).toBe(0)
|
||||
})
|
||||
|
||||
describe('absVal', () => {
|
||||
it('should return an absolute value of a negative number', () => {
|
||||
const absOfNegativeNumber = absVal(-34)
|
||||
const absOfNegativeNumber = abs(-34)
|
||||
expect(absOfNegativeNumber).toBe(34)
|
||||
})
|
||||
|
||||
it('should return an absolute value of a positive number', () => {
|
||||
const absOfPositiveNumber = absVal(50)
|
||||
const absOfPositiveNumber = abs(50)
|
||||
expect(absOfPositiveNumber).toBe(50)
|
||||
})
|
||||
|
||||
it('should return an absolute value of a zero number', () => {
|
||||
const absOfPositiveNumber = absVal(0)
|
||||
const absOfPositiveNumber = abs(0)
|
||||
expect(absOfPositiveNumber).toBe(0)
|
||||
})
|
||||
|
||||
it('should return an absolute value of any floating number', () => {
|
||||
const absOfPositiveNumber = abs(-20.2034)
|
||||
expect(absOfPositiveNumber).toBe(20.2034)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user