Loop

Hi,

Please help me in below code, I need “optionCount” should repeat “x” times

var participants= [];

            for (var j= 1; j<= k;  j++) {
                
                var option= [];

               for (x = 1; x <= i; x++) {

                   
                    option.push(x);


                    // alert(x);

                };

                    participants.push({
                    NoPrticiField: NoPrticiField,
                    empname: ko.observable(),
                    designation: ko.observable(),
                    email: ko.observable(),
                    optionCount: option,

                 // I need "optionCount" should repeat "x" times
                    
                });
               
            }

The following script writes the result of building the arrays to the page. It shows the outer loop value of J and the inner loop progressive values of the array of X values for each J. What you have here is an array of arrays - something like this [ [1,2,3…], [1,2,3…], [1,2,3…] … etc ].

I am uncertain what you mean by giving you with an option count. For instance you may mean how many inner arrays are there, or you might mean how many x values across all inner arrays are there. I will leave it to you to interpret the result from the output provided.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Loop</title>
<script type="text/javascript">
 var participants= [];
 var optionArray= new Array();
 var j=1, k=5, x=1, y=5;
 var build="";
 for (var j= 1; j<= k;  j++)
  { optionArray[j]=new Array();
    build+='<br>j= '+j+'<br>';
    for (x = 1; x <= y; x++)
       {  optionArray[j].push(x);
          build+=optionArray[j]+'<br>';
        }
  }
// -------
function result(){ document.getElementById("wrap").innerHTML=build; }
//
 window.onload=result;
</script>
<style type="text/css">
body   { font-family:arial, helvetica, sans-serif; font-weight:normal; font-size:13px; color:#000; text-align:left; margin:3px 0px; }
#wrap  { width:500px; height:500px; margin-left:50px;  }
</style>
</head>

<body>

<div id="wrap">
<!-- will write result to here -->
</div>

</body>

</html>


Hi Allan,

How can i push the two array, can you please help me in my below code. Thanks in advance


            for (var NoPrticiField = 1; NoPrticiField <= selectPartiNo; NoPrticiField++) {
                
               optionArray[NoPrticiField] = new Array();
              
               
               for (x = 1; x <= i; x++) {

                    
                    optionArray[NoPrticiField].push(x);
                    //alert(x);

                };

                    participants.push({
                    NoPrticiField: NoPrticiField,
                    empname: ko.observable(),
                    designation: ko.observable(),
                    email: ko.observable(),
                    optionCount:optionArray[NoPrticiField].push(x),  // I need to repeat this item "var x" times which is from above loop 
                    
                });
               
            }

Sorry, I am unable to help you further as I don’t see how the participants.push object and the loop above it work together.

AllanP, Do you have any other idea to make this possible ?

Write a list of the steps that are needed to achieve your required outcome. As an example you might say

  • Loop through participant array from i=0 to end
  • For each value of i create an object containing 5 X values generated in a second loop
  • Do something with the array
    etc …

It will not be possible for you to solve your problem unless you clearly set out the logic of what you are trying to achieve.