Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 765 Bytes

round-up-to-the-next-multiple-of-5.md

File metadata and controls

40 lines (30 loc) · 765 Bytes

Round up to the next multiple of 5 7 Kyu

LINK TO THE KATA - FUNDAMENTALS

Description

Given an integer as input, can you round it to the next (meaning, "greater than or equal") multiple of 5?

Examples:

input:    output:
0    ->   0
2    ->   5
3    ->   5
12   ->   15
21   ->   25
30   ->   30
-2   ->   0
-5   ->   -5
etc.

Input may be any positive or negative integer (including 0).

You can assume that all inputs are valid integers.

Solution

const roundToNext5 = n => {
  if (n < 0) return n % 5 === 0 ? n : n - (n % 5)

  return n % 5 === 0 ? n : n + (5 - (n % 5))
}