SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Loop

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop

    Hi,

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



    Code:
    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
                        
                    });
                   
                }
    Last edited by Pullo; Mar 4, 2014 at 05:51.

  2. #2
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    <!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>

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Allan,

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

    Code:
                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 
                        
                    });
                   
                }

  4. #4
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AllanP, Do you have any other idea to make this possible ?

  6. #6
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •