Repository navigation
#053 Return Negative
Create a function that takes a number as an argument and returns negative of that number.
Return negative numbers without any change.
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/053 Return Negative.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.
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 takes a number as an argument and returns negative of that number.
//Return negative numbers without any change.
function returnNegative(n) {
if (n === 0) return 0;
return n < 0 ? n : -n;
}
import * as Test from '../testHelper.js';
Test.assertEquals(returnNegative(4), -4)
Test.assertEquals(returnNegative(15), -15)
Test.assertEquals(returnNegative(-4), -4)
Test.assertEquals(returnNegative(42), -42)
Test.assertEquals(returnNegative(-9), -9)
Test.assertEquals(returnNegative(0), 0)
Test.assertEquals(returnNegative(1), -1)
Test.assertEquals(returnNegative(-1), -1)Cross-language explanation preserved from the study materials
Reference file: #047 Return Negative.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.
# [Return Negative](https://edabit.com/challenge/tYMRxtwDZAeyGnqaL)
Published by [Einy](https://edabit.com/user/5f6oSeq4r5WTXLdCQ)
language_fundamentals logic numbers
Create a function that takes a number as an argument and returns negative of that number. Return negative numbers without any change.
`returnNegative(4) ➞ -4`
`returnNegative(15) ➞ -15`
`returnNegative(-4) ➞ -4`
`returnNegative(0) ➞ 0`
### Time Complexity:
The time complexity of this function is _O(1)_. The function performs a single comparison (`n > 0`) and either returns `-n` or `n` based on the result.
### Space Complexity:
The time complexity of this function is _O(1)_. The function only uses a constant amount of space for the parameter n and the return value.

//Create a function that takes a number as an argument and returns negative of that number.
//Return negative numbers without any change.
using BenchmarkDotNet.Attributes;
using System;
namespace Challenges
{
public class Program47
{
public static int ReturnNegative(int n) => n < 0 ? n : -n;
}
public class BenchmarkProgram47
{
[Benchmark]
[Arguments(-4)]
[Arguments(212)]
[Arguments(-125)]
[Arguments(932123)]
public int ReturnNegative(int n) => Program47.ReturnNegative(n);
}
}
using Challenges.Edabit;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class Tests47
{
[Test]
[TestCase(-4, -4)]
[TestCase(212, -212)]
[TestCase(-125, -125)]
[TestCase(932123, -932123)]
public void ReturnNegative(int n, int expectedResult)
{
int result = Program47.ReturnNegative(n);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}