Forgive me if this is to basic or have been asked already, but I'm stuck at this problem and I feel like it must be something simple I'm not seeing.
I want to add all numbers in the array using recursion(done!) and I'm missing a statement to ignore all other types of values. For example:
var arr = [1,'a','b',2,[1],'c']
sumValues(arr) // => 4 .
function sumValues(arr){
if(arr.length === 0){
return 0;
} // if the array is empty
if(arr.length > 0){
if(Array.isArray(arr[0])){
return sumValues(arr[0]);
} // if the next element is an array
return arr.shift() + sumValues(arr);
}
}
arraySuminstead ofsumValues.