javascript - Returning true in recursive function when atleast 1 condition is met -


aspiring programmer here, whilst doing challenges on coderbyte came across little problem can't seem solve. challenge follows;

  • find largest value in array , check if combinations of other ints (excluding largest number) amounts largest number. return true if combination has been found, false otherwise.

i got parts of question down, except returning part. you'd think it's easiest part.. not me though. here's code:

function arrayadditioni(arr, largest, partial) {       var n, sumpart, remaining, largesti, result;      // declace next partial array, or start empty 1     partial = partial || [];      // find largest value original array,     //  use existing 1 if declared     largest = largest || arr.reduce(function(max, cur) {     if(cur>max) return cur;         else return max;     });      // sum possible combinations of partial array     sumpart = partial.reduce(function (a, b) {return + b;}, 0);      // sumpart equal largest int? return true     if(sumpart === largest) {         console.log("%s=%s", partial.join("+"), largest);         return true;     }      // sumpart more desired number? stop     if(sumpart > largest) {         console.log("%s>%s", partial.join("+"), largest)         return;     }      for(var i=0;i<arr.length;i++) {         n = arr[i];         if(n != largest) {             remaining = arr.slice(i + 1);             arrayadditioni(remaining, largest, partial.concat([n]));         };     };  return false; };  // keep function call here  // see how enter arguments in javascript scroll down arrayadditioni([4,6,23,10,1,3]); 

it logs correct answer(s) according first -if statement-, however, never logs "true" no matter seem do. on 1 hand can see returning false everytime since it's @ end of function. on other hand shouldn't return true meets true? had problem before, time find out how see this, , thereby fix it...


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 -