← Back to challenges

Recreating the abs() Function

JavaScriptHardlanguage_fundamentalsnumbersmath

Instructions

The Math.abs() function returns the absolute value of a number. This means that it returns a number's positive value. You can think of it as the distance away from zero.

Create a function that recreates this functionality.

Examples

absolute(-1.217197940) ➞ 1.21719794

absolute(-12.1320) ➞ 12.132

absolute(-544.0) ➞ 544

absolute(4666) ➞ 4666

absolute(-3.14) ➞ 3.14

absolute(250) ➞ 250

Notes

  • Tests will only include valid numbers.
  • Don't use the Math.abs() function (it will defeat the purpose of the challenge).
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Is the Last Character an "N"?