Adding all number combinations to an array

I am trying to write a program in javascript that gets an indefinite number of numbers from html textarea and tries all combinations (adding all numbers with eachother) to see if this corrects the number you specified.

Now I can make an array from a string in a text box and use for loops. I add them (see below code). The problem is, how can you do this for an undetermined number of numbers to be added (for example, adding 7 different numbers if you enter 7 numbers in the text box)? I was thinking about using a second array and which gets the numbers that should be added from the first loop. And then create the length of the loop variable using a for loop with the length of the array containing all the numbers (strings in my example) as the value of endvalue.

How can I populate the values โ€‹โ€‹of this second array, making sure all combinations are used?

By the way, I need this code because I'm an auditor. Sometimes a customer cancels a couple of amounts in one reservation without any comments. This code will make it easier to check which orders have been canceled.

edit: The cheek box seems to work, I have only one remark. What if a few subsets of your power set are added, then the number you are looking for? for example: findSum ([1,2,3,4,5], 6), can be obtained [1,2,3], but also [2,4] or [1,5]. Is it possible for a function to return multiple subsets?

Found the answer: "I myself" :) I replaced the code

 return numberSet; 

By

 document.getElementById("outp").value=document.getElementById("outp").value+ numberSet +"\n"; 

Thank you very much cheeken

Another additional question. How can I format the input to parse this function? The code below does not work. inp is the identifier of the text field where the input is located (numbers are separated by a half-ring point). The ge variable works, so there are no problems (tested with [1,2,3,4], and it worked. With this code?

re edit:

found a solution. The array should have been parsed as a floating-point number added by this code. `

 for (var i=0; i < lines.length; i++) { lines[i]= parseFloat(lines[i]); } findSum(document.getElementById("inp").value.split(";"), ge); 

The code:

 <!DOCTYPE html> <html> <head> <script type="text/javascript"> function powerset(arr) { var ps = [[]]; for (var i=0; i < arr.length; i++) { for (var j = 0, len = ps.length; j < len; j++) { ps.push(ps[j].concat(arr[i])); } } return ps; } function sum(arr) { var total = 0; for (var i = 0; i < arr.length; i++) total += arr[i]; return total } function findSum(numbers, targetSum) { var numberSets = powerset(numbers); for (var i=0; i < numberSets.length; i++) { var numberSet = numberSets[i]; if (sum(numberSet) == targetSum) document.getElementById("outp").value=document.getElementById("outp").value+ numberSet +"\n"; } } function main() { ge= document.getElementById("getal").value; findSum([1,1,0.5,0.1,0.2,0.2], ge); } </script> </head> <body> <input type="button" onclick="main()" value="tel" /><input type="text" id="getal" /><br> input<br><textarea id="inp" ></textarea><br> output<br><textarea id="outp" ></textarea><br> document.getElementById("inp").value.split(";") </body> </html> 
+1
javascript arrays
source share
1 answer

More specifically, you are looking for a specific amount of each set in the power set of your collection of numbers.

You can do this with the next bit of code.

 function powerset(arr) { var ps = [[]]; for (var i=0; i < arr.length; i++) { for (var j = 0, len = ps.length; j < len; j++) { ps.push(ps[j].concat(arr[i])); } } return ps; } function sum(arr) { var total = 0; for (var i = 0; i < arr.length; i++) total += arr[i]; return total } function findSum(numbers, targetSum) { var numberSets = powerset(numbers); for (var i=0; i < numberSets.length; i++) { var numberSet = numberSets[i]; if (sum(numberSet) == targetSum) return numberSet; } } 

Call example:

 >> findSum([1,2,3,4,5],6) [1, 2, 3] >> findSum([1,2,3,4,5],0) [] >> findSum([1,2,3,4,5],11) [1, 2, 3, 5] 

If you want to collect all subsets whose sum is a value (and not the first, as implemented above), you can use the following method.

 function findSums(numbers, targetSum) { var sumSets = []; var numberSets = powerset(numbers); for (var i=0; i < numberSets.length; i++) { var numberSet = numberSets[i]; if (sum(numberSet) == targetSum) sumSets.push(numberSet); } return sumSets; } 

Call example:

 >> findSums([1,2,3,4,5],5); [[2,3],[1,4],[5]] >> findSums([1,2,3,4,5],0); [[]] 
+3
source share

All Articles