0

I have an issue with average of total. I need to calculate average of sum with the help of following formula. following code has no calculate last column of Average. I have require desired result. Could you please help me. Helps are definitely appreciated.

Formula

colTotal/(names[$index].arr_length*4) 

var arr = [{
  "unique_id": "CS", "college": "BSCS", "arr_length": 1,
  "program_section": [
    { "question": "Q1", "total": 135 },
    { "question": "Q2", "total": 142 },
    { "question": "Q3", "total": 135 },
    { "question": "Q4", "total": 137 }
  ]
}, {
  "unique_id": "MBA", "college": "BBA", "arr_length": 2,
  "program_section": [
    { "question": "Q1", "total": 175 },
    { "question": "Q2", "total": 142 },
    { "question": "Q3", "total": 165 },
    { "question": "Q4", "total": 137 }
  ]
}, {
  "unique_id": "CA", "college": "Account", "arr_length": 1,
  "program_section": [
    { "question": "Q1", "total": 145 },
    { "question": "Q2", "total": 162 },
    { "question": "Q3", "total": 125 },
    { "question": "Q4", "total": 117 }
  ]
}];
angular.module('myApp', []).controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
    $scope.totals = {};
    arr.forEach(function(obj){
        obj.program_section.forEach(function(section){
            $scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
        });
    });

    // Sum each item in `arr`.
    $scope.colTotals = $scope.names.map(function (name) {
        return name.program_section.reduce(function (total, section) {
            return total + section.total;
        }, 0);
    });

    // Add the sum of sums
    $scope.colTotals.push($scope.colTotals.reduce(function (x, y) {
        return x + y;
    }, 0));
}).filter('range', function() {
  return function(input, total) {
    total = parseInt(total);

    for (var i=0; i<total; i++) {
      input.push(i);
    }

    return input;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table  border="1">
<thead>
  <tr>
    <td>Question</td>
    <td ng-repeat="x in names">{{ x.college }}</td>
    <td>Totals</td>
  </tr>
</thead>
<tbody>
      <tr ng-repeat="n in [] | range:4">
        <td>
            {{ names[0].program_section[n].question }}
        </td>
        <td width="100px" ng-repeat="x in names" >
           {{ x.program_section[n].total }}
        </td>
        <td width="50%" ng-bind="totals[names[0].program_section[n].question]"></td>
      </tr>
      <tr style="font-weight: bold;">
        <td>Totals</td>
        <td ng-repeat="colTotal in colTotals track by $index">{{ colTotal/(names[$index].arr_length*4) }}</td>
      </tr>
</tbody>
  
</table>

</div>

Desired Result

<table border="1">
  <thead>
    <tr>
      <td>Question</td>
      <td >BSCS</td>
      <td >BBA</td>
      <td >Account</td>
      <td>Totals</td>
    </tr>
  </thead>
  <tbody>
    <tr >
      <td> Q1 </td>
      <td width="100px"> 135 </td>
      <td width="100px"> 175 </td>
      <td width="100px"> 145 </td>
      <td width="50%">455</td>
    </tr>
    <tr >
      <td> Q2 </td>
      <td width="100px"> 142 </td>
      <td width="100px"> 142 </td>
      <td width="100px"> 162 </td>
      <td width="50%">446</td>
    </tr>
    <tr >
      <td> Q3 </td>
      <td width="100px"> 135 </td>
      <td width="100px"> 165 </td>
      <td width="100px"> 125 </td>
      <td width="50%">425</td>
    </tr>
    <tr >
      <td> Q4 </td>
      <td width="100px"> 137 </td>
      <td width="100px"> 137 </td>
      <td width="100px"> 117 </td>
      <td width="50%">391</td>
    </tr>
    <tr>
      <td>Totals</td>
      <td >137.25</td>
      <td >77.375</td>
      <td >137.25</td>
      <td >1717/4 =<strong> 429.25</strong></td>
    </tr>
  </tbody>
</table>

1 Answer 1

1

try this.when index is 3 then names[3].arr_length is undefind.

 <td ng-repeat="colTotal in colTotals track by $index" ng-if="!$last">{{ colTotal/(names[$index].arr_length*4) }}</td>
 <td ng-repeat="colTotal in colTotals track by $index" ng-if="$last">{{ colTotal/4 }}</td>
Sign up to request clarification or add additional context in comments.

Comments

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.