From 376a67cafbd6583ef34a788fc0d4a809d16d6ed3 Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Tue, 2 Jul 2019 09:35:06 -0400 Subject: [PATCH] Create abs.js This script calculates absolute value. --- maths/abs.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 maths/abs.js diff --git a/maths/abs.js b/maths/abs.js new file mode 100644 index 000000000..57d5acd5a --- /dev/null +++ b/maths/abs.js @@ -0,0 +1,26 @@ +/* + author: PatOnTheBack + license: GPL-3.0 or later + + Modified from: + https://github.com/TheAlgorithms/Python/blob/master/maths/abs.py + + This script will find the absolute value of a number. + + More about absolute values: + https://en.wikipedia.org/wiki/Absolute_value +*/ + +function abs_val(num) { + // Find absolute value of `num`. + "use strict"; + if (num < 0) { + return -num + } + // Executes if condition is not met. + return num +} + +// Run `abs` function to find absolute value of two numbers. +console.log("The absolute value of -34 is " + abs_val(-34)); +console.log("The absolute value of 34 is " + abs_val(34));