Skip to the content

The sign of a number

Problem

Write a function that takes a number and returns its sign: 1 if it's positive and -1 if it's negative. With 42 you return 1, with -3 you return -1.

Zero is neither positive nor negative, so it gets its own answer: with 0 you return 0. The number can have decimals: 0.5 is positive too.

Examples

  • 42 → 1

  • -3 → -1

  • 0 → 0

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def sign(n):
    pass

JavaScript

function sign(n) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(1) challenges

See all challenges →