Hi, guys!
It is my second week after 1 year of break in learning Java Script.
I’ve found this concept a bit difficult for me (I’ve spent whole day) to find a solution and I’d like to read your feedback about my version of this solution.
Thanks, a lot. )
function sum(arr, n) {
/********* FINAL SOLUTION RESULT: 0,2,9*********/var numb = arr.length -1; //base case if( arr.length <= 0){ return 1; //recursive function }else if( arr.length >= 0 ){ return sum(arr.length - 1, n), numb * n; } }
console.log(sum([1],0)); console.log(sum([2, 3, 4], 1)); console.log(sum([2, 3, 4, 5], 3));
/********** FINAL SOLUTION **********/ //********************************************** /* TRY WITHOUTH EXTERNAL VARIABLE. RESULT IS 0,3,12 */ /* if(arr.length <= 0){ return 0; }else if(arr.length >= 0){ return sum(arr.length - 1, n), arr.length * n; } */ //********************************************** /* TRY USING FOR LOOP */ //I've tried a for loop to find a solution before a recursive approach /*for(i = 0; i < arr.length; i++){ var sum = arr.length - 1; return sum * n; }*/