MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / abs

Function abs

Maths/Abs.js:12–20  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

10 */
11
12const abs = (num) => {
13 const validNumber = +num // converted to number, also can use - Number(num)
14
15 if (Number.isNaN(validNumber) || typeof num === 'object') {
16 throw new TypeError('Argument is NaN - Not a Number')
17 }
18
19 return validNumber < 0 ? -validNumber : validNumber // if number is less than zero means negative, then it converted to positive. i.e., n = -2 = -(-2) = 2
20}
21
22export { abs }

Callers 1

Abs.test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected