DEV Community

Aishe Ibrahim
Aishe Ibrahim

Posted on

CodeWars Blog #1

The problem:

In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?

This problem is asking for a program that will take a number and return the same number as a negative. If the number already entered is negative, then it negates the program.

To counteract that issue, Math.abs() method will first take the number and convert it to its absolute form (if it's negative, it will return positive) then multiply by (-1).

The Solution:

function makeNegative(num){
  return Math.abs(num) * -1
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay