Actual result not expected - two square roots

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?

Depending on the environment, console.log will shorten results. In most browsers, you’ll be able to click on the Array(2) part to expand it and see the contents.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.