diff --git a/Maths/Abs.js b/Maths/Abs.js index ed90edb34d..a418ee35f3 100644 --- a/Maths/Abs.js +++ b/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 } diff --git a/Maths/test/Abs.test.js b/Maths/test/Abs.test.js index 34143dfee4..6a67fa50d1 100644 --- a/Maths/test/Abs.test.js +++ b/Maths/test/Abs.test.js @@ -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) + }) }) 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