Math.min()
can accept arguments like Math.min(-2, -3, -1)
or with an array like Math.min(...arr)
. In this case, you are passing the arguments as
Math.min([14,99, 10, 18,19, 11, 34], 0)
where 0 is the minimum value. Therefore, the output would be 0. Since
Math.min()
already finds the minimum value, there's no need for a loop. The solution can be written as:
function values(ar) {
var min_val = Math.min(...ar);
//document.write(min_val);
return min_val
}
console.log(values([14,99, 10, 18,19, 11, 34]))
Alternatively (to match your specific case):
function values(ar) {
var min_val = Math.min(...ar);
document.write(min_val);
}
In its shortest form:
document.write(Math.min(...ar));