Skip to the content

How far off is it?

Problem

A suitcase is only accepted if it weighs between low and high kilos, both ends included. Write a function that returns how far the weight is outside that range: 25 in a range of 10 to 20 gives you 5, and 7 gives you 3. If the weight is inside you return 0, and sitting right on one of the ends counts as inside. What you return is never negative.

Examples

  • Over the top

    25, 10, 20 → 5

  • Under the bottom

    7, 10, 20 → 3

  • Inside the range

    15, 10, 20 → 0

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

You start with this

Python

def off_by(value, low, high):
    pass

JavaScript

function offBy(value, low, high) {
}
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 →