closestMultiple10
codewars kata 7
by trentHarlem
JavaScript
const closestMultiple10 = num => Math.round(num / 10) * 10;
/* const closestMultiple10 = num => Math.round(num / 10) * 10; */
/* console.log(
Math.round(22.4),
Math.round(22.4 / 10),
Math.round(22.4 / 10) * 10
) */
// over think much?!
/* const closestMultiple10 = num => {
num = Math.round(Math.trunc(num))
let P = num;
let M = num;
let result;
if (num % 10 === 0) {
return num;
} else {
const test = (P, M) => {
P++;
M--;
(P % 10 === 0) ? result = P: (M % 10 === 0) ? result = M : test(P, M);
};
test(P, M);
}
return result;
};
*/
/* const test = (P, M) => {
P++;
M--;
(P % 10 === 0) ? result = P : (M % 10 === 0) ? result = M : test(P, M);
};
const closestMultiple10 = num => {
let P = num;
let M = num;
(num % 10 === 0) ? num : test(P, M);
}; */
console.log(
closestMultiple10(14.4),
closestMultiple10(22),
closestMultiple10(25),
closestMultiple10(37),
closestMultiple10(54),
closestMultiple10(56),
)