I've defined the function prod
which makes use of ES6's default arguments and destructuring:
function prod([a, b, c] = [1, 2, 3]) {
console.log(a * b * c);
}
When called without arguments, it logs 6
to the console as expected.
prod() // 6
When called with an array of arguments, it logs the correct product:
prod([2, 3, 4]) // 24
When called with a number of arguments, it throws an error:
prod(2, 3, 4) // Uncaught TypeError: undefined is not a function(…)
Why does it throw an undefined is not a function
error?
Edit
I understand why it throws an error. What I don't understand is why it throws that particular error.
The issue is that it wants an array for arguments, not 3 separate arguments. So doing this: prod([1, 2, 3])
will give you back your expected answer.
When I run prod(1, 2, 3)
in Firefox I get this error: TypeError: [Symbol.iterator] is not a function
.
According to the mdn docs (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#Non-well-formed_iterables)
The function is expecting an interable object to be returned with the method @@iterator. Because that method doesn't exist (and therefore isn't a function) you're getting the is not a function error