I'm writing my first lines of JS code, now I'm trying to do an easy recursive function.
I know splice() and shift() removes the item they extract.
I'm having troubles here:
function sub (...par) { // It could take an arbitrary number of parameters
if (par.length >= 2)
{
var val = par.shift();
//val = par.splice(0,1);
return val - sub(par);
}
else
{
/*EDIT*/ //return par[0];
return -par[0];
}
}
Now if I run: sub (17,7) I correctly got 10, but If I run sub(17,7,3) instead of getting 8 I got NaN.
fixed like this thanks to Jonas Wilms help:
function sub (...par) { // It could take an arbitrary number of parameters
if (par.length >= 2)
{
var val = par.shift();
//val = par.splice(0,1);
return val - sub(...par);
}
else
{
/*EDIT*/ //return par[0];
return -par[0];
}
}
You have to spread on the recursive call:
return val - sub(...par);
Otherwise par inside of the called sub will be an array containing an array, and doing substraction on that will fail.
However the whole thing is way more elegant if you take the first parameter directly:
function sub(head, ...rest) {
if(!rest.length) return head;
return head - sub(...rest);
}
However one should note that you might not get the result you expect with that, as you calculate the right side before the left, so
sub(1, 2, 3)
equals
1 - (2 - 3)
and thats 2.
I think it would be clearer if sub(1, 2, 3) would equal 1 - 2 - 3 that can be achieved with:
function sub(...rest) {
if(rest.length === 1) return rest[0];
const last = rest.pop();
return sub(...rest) - last;
}
Or without recursion:
const sub = (...args) => args.reduce((a, b) => a - b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With