← Back to challenges

Negate the Array of Numbers

JavaScriptHardarraysnumberslanguage_fundamentalsloops

Instructions

Given an array of numbers, negate all elements contained within.

  • Negating a positive value -+n will return -n, because all +'s are removed.
  • Negating a negative value --n will return n, because the first - turns the second minus into a +.

Examples

negate([1, 2, 3, 4]) ➞ [-1, -2, -3, -4]

negate([-1, 2, -3, 4]) ➞ [1, -2, 3, -4]

negate([]) ➞ []

Notes

If you get an empty array, return an empty array: []

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Return the Four Letter Strings