I recently did a coding challenge in checkio.org and had a bit of trouble with the Math.max and min functions. I eventually passed it using Math.max.apply(arguments).
When I looked at the top voted solution I noticed that they simply prefixed the parameter with ‘…’ in both the function definition and as the argument for Math.max etc and bypassed the need to use apply(). What exactly do those three dots do?
My solution:
"use strict";
function mostNumbers(numbers) {
return numbers ? Math.max.apply(null, arguments) - Math.min.apply(null, arguments) : 0;
}
Math.round(mostNumbers(10.2, -2.2, 0, 1.1, 0.5) * 1000) // 12400
top solution:
"use strict";
function mostNumbers(...args){
return args.length === 0 ? 0 : Math.max(...args) - Math.min(...args)
}