← Back to challenges

Round to Closest N

JavaScriptHardnumberslogicarrays

Instructions

Create a function that takes two integers, num and n, and returns an integer which is divisible by n and is the closest to num. If there are two numbers equidistant from num and divisible by n, select the larger one.

Examples

roundNumber(33, 25) ➞ 25

roundNumber(46, 7) ➞ 49

roundNumber(133, 14) ➞ 140

Notes

n will always be a positive number.

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