Skip to content

Improved abs function #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Maths/Abs.js
Original file line number Diff line number Diff line change
@@ -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 }
31 changes: 26 additions & 5 deletions Maths/test/Abs.test.js
Original file line number Diff line number Diff line change
@@ -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