0

I am having trouble with getting the total amount of price when a list of item is selected by the checkbox. With this for loop, all I get is the first two dollar amount (I know it is the selectedTotal += (getAmount.amount + getAmount.amount); problem, but not sure how to fix it), I cannot get total price amount when there are more then three selected list item. tempData is array var tempData = [] Help will be appreciated.

HTML

 <label class="item item-input ">
    <b class="input-label">Total Amount: </b>
    <span style="margin-left:30%;"> ${{getTotal()}} </span>
 </label>

CONTROLLER

$scope.getTotal = function () {
    var selectedTotal = 0;
    for (var i = 0; i < $scope.tempData.length; i++) {
        if ($scope.tempData[i].checked) {
            var getAmount = $scope.tempData[i];
            selectedTotal += (getAmount.amount + getAmount.amount);
        }
        return selectedTotal;
    }
}
SK.
  • 4,174
  • 4
  • 30
  • 48
Big Ticket
  • 503
  • 3
  • 5
  • 17
  • Why do you have `selectedTotal += (getAmount.amount + getAmount.amount);` (getAmount.amount twice) ? – Shahar Jul 28 '15 at 09:35
  • I got it now, I remove the second getAmount.amount and then put the return selectedTotal outside of the forloop at it works now – Big Ticket Jul 28 '15 at 09:40

2 Answers2

2

Try to return selectedTotal outside of for loop.

rundelis
  • 35
  • 8
2

You have to put return selectedTotal outside the loop otherwise it will return value after every iteration of the loop.

$scope.getTotal = function () {
    var selectedTotal = 0;
    for (var i = 0; i < $scope.tempData.length; i++) {
        if ($scope.tempData[i].checked) {
            var getAmount = $scope.tempData[i];
            selectedTotal += (getAmount.amount + getAmount.amount);
        }
    }
return selectedTotal;
}

Read here: Return in for loop or outside loop

Community
  • 1
  • 1
SK.
  • 4,174
  • 4
  • 30
  • 48