Suppose I have this simple JavaScript function:
function returnArray(){
return [1, 2, 3];
}
Further suppose that I then say
<
Just a minor change:
function double(array) {
// note the return here is an array, not a number
if (array.length === 1) return [2 * array[0]];
var [num, ...rest] = array;
if (rest.length) return [2 * num, ...double(rest)];
}
console.log(double([1, 2, 3, 4]));
You were returning a number, and destructuring a number will leave you with an error.
...5 // throws SyntaxError