From 0924f1c8c9eb52e9a241cdd7c0c1138d83b16b48 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal <57553028+fahimfaisaal@users.noreply.github.com> Date: Tue, 15 Mar 2022 18:11:50 +0600 Subject: [PATCH] merge: Improved `abs` function (#923) * feat: added validation, test codes * chore: remove useless words --- Maths/Abs.js | 30 +++++++++++++++--------------- Maths/test/Abs.test.js | 31 ++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Maths/Abs.js b/Maths/Abs.js index ed90edb34..a418ee35f 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 34143dfee..6a67fa50d 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) + }) })