← Back to challenges

Round to the Nearest

JavaScriptHardalgorithmsmathnumbers

Instructions

Create a function that takes two numbers as arguments and returns the first number rounded to the nearest number, or a multiple of that number.

Examples

roundNearest(17, 10) ➞ 20

roundNearest(36.3) ➞ 36

roundNearest(123, 5) ➞ 125

Notes

If no nearest number is given, round the number as you normally would (see example #2).

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.