Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:51:52+00:00 2026-06-10T03:51:52+00:00

I am trying to write a program in javascript that gets an unspecified number

  • 0

I am trying to write a program in javascript that gets an unspecified number of numbers out of a html textarea and tries all combinations (adding all numbers with eachother) to see if it mathches a number you specified.

Now I can make an array out of the string in the textarea and using for loops I add these up (see below code). The problem how can you do this for an unspecified number of numbers that are to be added up (e.g. adding up 7 different number if you enter 7 numbers in textarea)? I was thinking of using a second array and, which gets the numbers to add up out of the first loop. And then make te lenght of the loop variable by using a for loop with the lenght of the array containing all numbers (lines in my example) as endvalue.

How can I fill in the values of this 2nd array, making sure all combinations are used?

By the way, I wanted this code because I am a auditor. Sometimes a client reverses a couple of amounts in one booking, without any comment. This code will make it a lot easier to check what bookings have been reversed

edit: The awnser of cheeken seems to be working I only have one remark. What if multiple sub sets of your power set added up result in the number you are looking for? e.g.:findSum([1,2,3,4,5],6) can result [1,2,3] but also [2,4] or [1,5]. is it possible to let the function return multiple sub sets?

Found the answer my self 🙂
I replaced code

return numberSet;

By

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

Thank you very much Cheeken

One more additional question. How do i format the input for parsing that function? The code below doesn’t seem to work. inp is the ID of the textarea where the input is (the numbers are seperated with a semicolumn. The variable ge works so there is no problem there (tested it with [1,2,3,4] and it worked. What is wrong with this code?

re edit:

found the solution. The array needed to be parsed as a floating number added this code.`

for (var i=0; i < lines.length; i++) {
    lines[i]= parseFloat(lines[i]);
    }


findSum(document.getElementById("inp").value.split(";"), ge);

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:51:53+00:00Added an answer on June 10, 2026 at 3:51 am

    More concretely, you’re looking for a particular sum of each set in the power set of your collection of numbers.

    You can accomplish this with the following 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;
        }
    }
    

    Example invocation:

    >> 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’d like to collect all of the subsets whose sum is the value (rather than the first one, 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;
    }
    

    Example invocation:

    >> findSums([1,2,3,4,5],5);
    [[2,3],[1,4],[5]]
    >> findSums([1,2,3,4,5],0);
    [[]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that will take an HTML file and make
I'm trying to write a program that reads 2 numbers from the user and
gud day... i'm trying to write a program in javascript that uses .json. i
I'm trying to write a program that would convert celcius to fahrenheit and visa-versa.
I am trying to write a program that will prompt you to enter someone's
I am trying to write a program that sends basic text files from one
I am trying to write a program that allows a binary to be run,
I am trying to write a program that displays the integers between 1 and
I'm trying to write a program in Java that uses osql to generate a
i am trying to write a program that close explorer then runs another program.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.