0
<script>

   function generateCaseDetails(array, n)
   {
     row = '';

     for( i = 0;i<array.length;i=i+n)
     {
        row+=
        '<tr class = "'+(i%2==0 ? 'even' : '')+'">'+
         for(j = 0; j < n ; ++j)
         {
            '<td><label>'+array[i+j].label+'</label><div>'+array[i+j].value+'</div></td>'+

         }
        '</tr>'
     }

     document.write(row);
   }

   a = 12.5;
   b = 0.3;
   c = 3.4;
   d = 1.2;

   caseDetails = [
     {"label":"30 day exception ratio", "value":a},
     {"label":"30 day exception turn ratio", "value":b},
     {"label":"60 day exception ratio", "value":c},
     {"label":"60 day exception turn ratio", "value":d}
   ]

  generateCaseDetails(caseDetails, 2);
</script>

I'm trying to create rows and columns dynamically in a table using above method. It works only with the outer loop, i.e. keeping the columns fixed. I want the no. of columns to be dynamically decided based on the value passed in the parameter of the function. Somehow the inner loop is not working.

Am I doing a syntactical mistake ?

Thanks in advance!

EDIT

Expected output:

 30 day exception ratio  30 day exception turn ratio  
 12.5                    0.3   
 60 day exception ratio  60 day exception turn ratio  
 3.4                     1.2
4
  • What do you expect from array[i+j]? Commented Mar 13, 2016 at 15:23
  • also see :www.w3schools.com/jsref/dom_obj_table.asp Commented Mar 13, 2016 at 15:27
  • @omeinusch, I want the succeeding item in the array to be printed using array[i+j]. Commented Mar 13, 2016 at 15:27
  • 1
    Please add your expected output. Commented Mar 13, 2016 at 15:29

1 Answer 1

1

You are trying to concanate a string with a for-loop:

    row+=
    '<tr class = "'+(i%2==0 ? 'even' : '')+'">'+
     for(j = 0; j < n ; ++j)

This cannot work and your javascript console should give you a reasonable error message.

Just end the first line with the ;-terminator and prepend the string in the inner loop with row +=. Again, end with the ; instead of the +.

Of course, the lone </tr> at the end need the preceeding row +=, too.

Your inner loop should look like:

    row += '<tr class = "'+(i%2==0 ? 'even' : '')+'">';
     for(j = 0; j < n ; ++j)
     {
        row += '<td><label>'+array[i+j].label+'</label><div>'+array[i+j].value+'</div></td>';

     }
    row += '</tr>';

Nevertheless, I don't understand what do you want to achieve with array[i+j].

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @omeinusch for the quick reply. But this too is not working.
It's a step forward. Please add your expected output in your question as mentioned in my comment above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.