1

I'm quite knew to JavaScript (been studying it only for less than a week now) and am having problems computing the sum of the values in the subarrays I generated from a certain array. I was able to do that using this function:

function getSubs(arr) {
  var newarr = [];
  for (var i = 0; i < arr.length; i++){
    for (var j = arr.length; j > 0; j--){
      newarr.push(arr.slice(i, j));
    }
  }
  return newarr;
}

Now if this function was invoked for example for the array [1,2,3], the result is [[], [], [], [1], [2], [3], [2, 3], [1, 2], [1, 2, 3]]. I don't understand why there are three null arrays but this is close to what I'm trying to achieve. Additionally, I want to get the sums of the values in each subarray. I know the code above's very crude. Hope someone can help me improve it. Thanks in advance!

falsarella
  • 12,217
  • 9
  • 69
  • 115
dunkelmeee
  • 21
  • 2

1 Answers1

1

The condition for the nested for is incorrect. Change it from j > 0 to j > i, and it will work fine:

function getSubs(arr){
  var newarr = [];
  for (var i=0;i<arr.length;i++){
    for (var j=arr.length;j>i;j--){
      newarr.push(arr.slice(i,j));
    }
  }
  return newarr;
}

Input:

[1,2,3]

Output:

[[1,2,3],[1,2],[1],[2,3],[2],[3]]

Just to note: considering Array.slice(initialOffset, finalOffset), it makes sense to just return a not empty array when finalOffset > initialOffset.

falsarella
  • 12,217
  • 9
  • 69
  • 115
  • the empty set still needs to be included, since it is considered a subset https://www.google.com/search?q=power+set&ie=utf-8&oe=utf-8 – chiliNUT Feb 23 '15 at 19:11
  • @chiliNUT The OP told just to get rid of the empty subarrays. – falsarella Feb 23 '15 at 19:12
  • @chiliNUT Just to mention, if the OP really wants a power set, that code wouldn't retrieve `[1,3]` also. – falsarella Feb 23 '15 at 19:15
  • 1
    Thanks a lot! :) Now off to experiment how to get the sums... :) – dunkelmeee Feb 23 '15 at 19:16
  • 1
    @falsarella now that you mentioned it, yes, it doesn't really get that. i thought i had all the possible sub-arrays there. would you have any idea how I can improve the code to get that? – dunkelmeee Feb 23 '15 at 19:22
  • @falsarella you are right, I read the question as "all subsets of an array" which includes the empty array. @dunkelmee, what exactly are you looking for? Is `[[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3],[]]` the expected output? Or just `[[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]` – chiliNUT Feb 23 '15 at 19:32
  • @chiliNUT I don't want to include the empty array since that will not have a sum – dunkelmeee Feb 23 '15 at 19:46
  • word. i think a better title for the question would then be "Find all non-empty subsets of an array" – chiliNUT Feb 23 '15 at 19:50
  • @dunkelmeee I think it would suit another question, since it would need recursion, but before that, try it yourself and do some research, look, for example at http://stackoverflow.com/a/5752056/1064325! Also, here goes a tip: if you'll try it alone, make sure to test it with larger arrays! – falsarella Feb 23 '15 at 19:50