function roots(x) {
if (x < 0) {
return [] // No roots
} else if (x === 0) {
return [0] // Single root
} else {
return [Math.sqrt(x), -Math.sqrt(x)] // Two roots
}
}
console.log([-1, 0, 1, 4].map(roots))
I expect the result will be:
[[], [0], [1, -1], [2, -2]]
However, the actual result is:
(4) [Array(0), Array(1), Array(2), Array(2)]
Why?