javascript - Please help me understand this recursion function -


i'm trying understand recursion function , better educate myself on recursion in general. can't seem follow how code it's thing. i'm hoping can walk me through can understand what's going on. function takes largest value of array , checks see if combination of numbers add value.

function arrayadditioni(arr) {   arr.sort(function(a,b){  //sort array;     return - b;   });   console.log('sorted array: ['+arr+']');   var largest = arr.pop();  //grab last value aka largest    function recursion(target, array){     if(array.length === 0) {  //if array empty return boolean         return target === 0;     }     var n = array[0];       //first value of array     array = array.slice(1);     console.log('array: [' +array+']');     console.log('target: '+target);     return recursion(target,array) || recursion(target - n, array);   //not sure ???   }   return recursion(largest,arr);  //call recursion function }  console.log(arrayadditioni([4,6,21,10,20,1])); 

here results calling this. i've commented on them show lost.

sorted array: [1,4,6,10,20,21]  array: [4,6,10,20]  //first value , largest value removed target: 21          //target highest value array: [6,10,20]    //first value removed again target: 21          //still 21 array: [10,20]      //another value removed target: 21 array: [20]         //last value in array target: 21 array: []           //empty array.. why did not return false??? target: 21           array: []           //why doing twice? target: 11          //where in world did come from. see 21 - 10 how did happen? array: [20]         //when did value added array? target: 15          //totally lost @ point... array: [] target: 15 array: [] target: 5 array: [10,20]      //where these values coming from? target: 17 array: [20] target: 17          //why target keep changing?? array: []           target: 17 array: []            target: 7 array: [20] target: 11          //from 17 7 11? array: [] target: 11 array: []           //empty arrays still not returning false?? target: 1 array: [6,10,20]    //no idea how these combinations being generated. target: 20 array: [10,20] target: 20 array: [20] target: 20 array: [] target: 20 true                //right answer how? 

i hope gets on right track:

array: [] //empty array.. why did not return false???

because array wasn't empty when check done. became empty when slice call removed last item. calls follow return false when array empty going function.

array: [] //why doing twice?

because of expression recursion(target,array) || recursion(target - n, array). each of them called array single item, , show empty array when remove item.

target: 11 //where in world did come from. see 21 - 10 how did happen?

that's recursion(target - n, array) being called when target == 21 , n == 10.

array: [20] //when did value added array?

it's not added back, it's different array. recursion has reached limit , taking step previous state try next possibility.

target: 15 //totally lost @ point...

that's recursion(target - n, array) being called when target == 21 , n == 6.

array: [10,20] //where these values coming from?

that's step back. each recursion level stores state on stack, can previous state.

target: 17 //why target keep changing??

because code trying different combinations path of recursion(target - n, array) call. function called new value target.

array: [] //empty arrays still not returning false??

yes, do. each empty array ends 1 possible combination, , makes code , try next combination.

array: [6,10,20] //no idea how these combinations being generated.

by tracking state array [4,6,10,20] , slicing off 4.

true //right answer how?

by going every possible combination , out again until right 1 found.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -