Finding all possible combinations of numbers to reach a given sum JavaScript

Finding all possible combinations of numbers to reach a given sum JavaScript

Finding all possible combinations of numbers to reach a given sum JavaScript

Finding all possible combinations of numbers to reach a given sum JavaScript. In this Article , i will use JavaScript  for loop to generate different possible adding combination of 4 number specified by the user . The Combination numbers will add up  to the target sum specified by the User.

Function in JavaScript that i will be using is as below:

  1. comFunc -> Stands for Combination Functions
  2. geCom-> Stand sfor Generate Functions

I will Attached the Code below ,

The Code is heavily Commented , hopefully it will be easily understood

The APP

Document

This App will Generate Different Adding Combination

User are require to input 4 individual Numbers

User are require to Provide a Target Sum

The App will Generate Adding Combination . By adding the Number 3 Time base on the User Provided Set

Sample Code for Finding all possible combinations of numbers to reach a given sum JavaScript

var member =[];
// Main Container Array
var container=[];
// Combination Array
var arrays=[];
var target =0;
var text ="";

//-------------------Combination Function -----------------------------------------------------------
const comFunc =(ar_a,  tar)=>{
let temp =0;

let temp_2 =0;

// Loop Through the array
for(x=0; x < ar_a.length ; x++){

    // Check Whether the Target Sum equal to any member in the Array
    if (ar_a[x] == tar){
               // if yes push the Value into the Member Array

               member.push(ar_a[x]);
			   member.push(0);
		       member.push(0);
		   	   // Push the member into the container	   
		       container.push(member);
			    // Clear container
			   member =[];

    }
    // if does not equal
    else{
        // loop trough the array in second tier
        for (y=0; y <ar_a.length;y++){
              // add the first tier with the second tier 
            temp = ar_a[x] + ar_a[y];
            if (temp == tar){
                       	
                      member.push(ar_a[x]);
					  member.push(ar_a[y]);
					  member.push(0);
					  container.push(member);
					  member =[];


            }
            else{

                for (z=0; z <ar_a.length;z++){
                    // add the first tier with the second tier  and third Tier
                    temp_2 = ar_a[x] + ar_a[y] +ar_a[z];

                    if (temp_2 == tar){
                      member.push(ar_a[x]);
					  member.push(ar_a[y]);
					  member.push(ar_a[z]);
					  container.push(member);
					  member =[];



                    }


                }



            }




        }




    }









}



}


//--------------------Generate------------------------------------------------------------------------



const geCom =() =>{
    // Get all Value from the Input HTML Text
    let var_one = document.getElementById("var_1").value;
    let var_two = document.getElementById("var_2").value;
    let var_three = document.getElementById("var_3").value;
    let var_four = document.getElementById("var_4").value;
    target = document.getElementById("target").value;
    // Convert all text into Integer
    var_one=parseInt(var_one);
    var_two=parseInt(var_two);
    var_three=parseInt(var_three);
    var_four=parseInt(var_four);
    target=parseInt(target);
    // Push the Value into the Arrays
    arrays.push(var_one);
    arrays.push(var_two);
    arrays.push(var_three);
    arrays.push(var_four);


    // Call the function
    comFunc(arrays,target);

    // First loop through the container
    for(i=0; i< container.length ;i++){
        // within the container  loop through the child array
        for (y=0; y<container[i].length;y++){

           // Concatenate the text
            text += container[i][y] + " , ";

          

        }
        // assign break point for next line
        text += "<br/>";

    }

     // Print out the Result
    document.getElementById("outputArray").innerHTML = text;
     // Clear Everything
     member =[];
     container=[];
     arrays=[];
     target =0;
     text ="";


}

Check out How to use  for loop to create element and Print out Array Content in JavaScript Here

Leave a Reply

Your email address will not be published. Required fields are marked *

1 × one =