Skip to content

#055 Recreating Function

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

055 Recreating Function

Create a function that recreates Math.abs().

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/055 Recreating Function.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that recreates Math.abs().
function absolute(n) {
    if (n === 0) return 0;
    return n > 0 ? n : -n;
}
import * as Test from '../testHelper.js';
const excludes = f => !/Math\.abs/.test(f);
Test.assertNotEquals(excludes(absolute), false, "A restricted function is found!");

const [numVector, resVector] = [
    [-5, -3.14, 250, 0, 6.28, 11037, -12.1320, -1.217197940, 434.6022, -130.406, 26.35,
    -901.732, 868.543, -566.26, 41.05, -889.68, -746.2169, -761.19, 982.503, -733.379,
    -374.406, 601.091, -833.14, -205.7941, -985.92, -432.627, -624.0324, 450.144, 190.319,
        640.22, -468.0362, -7.3915, 554.34, -969.15, -892.875, -163.7127, 626.56, 244.125, -140.56,
    -388.5835, -658.64, -888.92, -689.364, -982.2118, 818.85, 685.15, -104.13, 767.341, -221.47,
    -763.2578, -75.3508, 166.0908, 521.422, -633.0451, -732.862, 372.371, -369.78, -820.2045,
        131.145, 154.64, 782.24, -136.921, -395.722, 229.325, 176.452, 740.12, -432.2624, -661.675,
        156.872, 749.95, -488.386, -659.25, 250.4192, 890.76, 761.384, 806.476, 506.94, 831.81,
    -169.69, -941.307, 821.302, -665.6106, 39.031, 825.04, 275.973, 929.141, -716.83, -428.563,
        132.8035, 381.26, -462.211, -249.6488, 702.74, 758.99, 361.37, -266.31, -239.4475, 705.7603,
    -538.2, -3.37, -589.1788, -797.1114, 748.28, 590.3861, 435.854, -983.71, -224.704, 142.48,
        521.8522, -740.261, 735.7873
    ], [
        5.0, 3.14, 250.0, 0.0, 6.28, 11037.0, 12.132, 1.21719794, 434.6022, 130.406, 26.35, 901.732, 868.543,
        566.26, 41.05, 889.68, 746.2169, 761.19, 982.503, 733.379, 374.406, 601.091, 833.14, 205.7941, 985.92,
        432.627, 624.0324, 450.144, 190.319, 640.22, 468.0362, 7.3915, 554.34, 969.15, 892.875, 163.7127, 626.56,
        244.125, 140.56, 388.5835, 658.64, 888.92, 689.364, 982.2118, 818.85, 685.15, 104.13, 767.341, 221.47,
        763.2578, 75.3508, 166.0908, 521.422, 633.0451, 732.862, 372.371, 369.78, 820.2045, 131.145, 154.64,
        782.24, 136.921, 395.722, 229.325, 176.452, 740.12, 432.2624, 661.675, 156.872, 749.95, 488.386, 659.25,
        250.4192, 890.76, 761.384, 806.476, 506.94, 831.81, 169.69, 941.307, 821.302, 665.6106, 39.031, 825.04,
        275.973, 929.141, 716.83, 428.563, 132.8035, 381.26, 462.211, 249.6488, 702.74, 758.99, 361.37, 266.31,
        239.4475, 705.7603, 538.2, 3.37, 589.1788, 797.1114, 748.28, 590.3861, 435.854, 983.71, 224.704, 142.48,
        521.8522, 740.261, 735.7873
    ]
]
for (const i in numVector) Test.assertEquals(absolute(numVector[i]), resVector[i])

Historical reference notes

Cross-language explanation preserved from the study materials

Reference file: #049 Recreating Function.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.

# [Recreating the abs() Function](https://edabit.com/challenge/nK57J5MtLWgGtecPx)
Published by [Werdna](https://edabit.com/user/buX4mCQhtXCfBjeno)

language_fundamentals math numbers

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.

`absolute(-3) ➞ 3`

`absolute(250) ➞ 250`

Tests will only include valid numbers.

Don't use the `Math.Abs()` function (it will defeat the purpose of the challenge).

### Time Complexity:
The time complexity of this function is _O(1)_. The function performs a constant number of operations (comparison and conditional return).

### Space Complexity:
The time complexity of this function is _O(1)_. The function uses a constant amount of space for the parameter `num` and the return value.
![flowchart](https://diagrams.helpful.dev/d-r2/d-r2:oCf0YDei)

//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.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
    public class Program49
    {
        public static int Absolute(int num) => (num ^ (num >> 31)) - (num >> 31);
    }
    public class BenchmarkProgram49
    {
        [Benchmark]
        [Arguments(-5)]
        [Arguments(-3)]
        [Arguments(250)]
        [Arguments(0)]
        [Arguments(6)]
        [Arguments(1103)]
        public int Absolute(int num) => Program49.Absolute(num);
    }
}

using Challenges.Edabit;
using NUnit.Framework;
namespace Tests
{
    [TestFixture]
    public class Tests49
    {
        [Test]
        [TestCase(-5, 5)]
        [TestCase(-3, 3)]
        [TestCase(250, 250)]
        [TestCase(0, 0)]
        [TestCase(6, 6)]
        [TestCase(1103, 1103)]

        public void FixedTest(int a, int expectedResult)
        {
            // Arrange
            int number = a;
            int result = Program49.Absolute(number);
            Assert.That(result, Is.EqualTo(expectedResult));
        }
    }
}

Clone this wiki locally