I’m going through the Functions chapter in Eloquent JavaScript, and I see this code:
function roundTo(n, step = 1) {
let remainder = n % step;
return n - remainder + (remainder < step / 2 ? 0 : step);
};
console.log(roundTo(4.5));
// → 5
console.log(roundTo(4.5, 2));
// → 4
I don’t understand what step
is. Also, I don’t understand what return n - remainder + (remainder < step / 2 ? 0: step)
is doing.
Could someone break this down for me?
I’ve tried searching Google but haven’t found the answer.
New contributor
Brian Suttles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1