1

I have my code below i wanted to know how will i get the count of all $scope.rm that is equal to failed or how many $scope.percentage < 50.

angular.forEach(result1, function (value, key) {

    $scope.percentage = (value.score * 100 / value.total).toFixed(2);

    if ($scope.percentage < 50) {
        $scope.rm = "Failed"
        $rootScope.sendmail = 0

    }else {
        $scope.rm = "Passed"
        $rootScope.sendmail = 1
    }

});
1
  • 1
    take a variable outside of forEach and assign it to zero after that increment it when you get $scope.rm = "Failed" then you'll get no. of loop execution for that condition Commented Mar 1, 2019 at 7:54

3 Answers 3

3

Take a variable outside of forEach and assign it to zero

after that increment it when you get $scope.rm = "Failed"

then you'll get no. of loop execution for that condition

for example:

var Failedcount=0,Passedcount=0;
angular.forEach(result1, function (value, key) {

       $scope.percentage = (value.score * 100 / value.total).toFixed(2);

                    if ($scope.percentage < 50) {
                        $scope.rm = "Failed"
                        $rootScope.sendmail = 0
                        Failedcount++;

                    }else {
                        $scope.rm = "Passed"
                        $rootScope.sendmail = 1
                        Passedcount++;
                    }

                });
console.log("failed counter",Failedcount)
console.log("passed counter",Passedcount)
Sign up to request clarification or add additional context in comments.

Comments

2
var failedCount = 0;

angular.forEach(result1, function (value, key) {
  $scope.percentage = (value.score * 100 / value.total).toFixed(2);  

  if ($scope.percentage < 50) {
     $scope.rm = "Failed"
     $rootScope.sendmail = 0
     failedCount++;
  }else {
     $scope.rm = "Passed"
     $rootScope.sendmail = 1
  }
});

console.log(' total failed count : '+failedCount);

1 Comment

Simplest solution is to put a counter.
1

Here I declared another variable to track the number of times it fails.

$scope.numberOfFails = 0

Then make it increment by 1 every time the fail condition is met, so it counts every time it fails.

$scope.numberOfFails = 0
angular.forEach(result1, function(value, key) {

  $scope.percentage = (value.score * 100 / value.total).toFixed(2);

  if ($scope.percentage < 50) {
    $scope.rm = "Failed"
    $rootScope.sendmail = 0
    $scope.numberOfFails++
  } else {
    $scope.rm = "Passed"
    $rootScope.sendmail = 1
  }

});
console.log($scope.numberOfFails)

After that, you will get $scope.numberOfFails as the correct number of times it has failed.

Comments

Your Answer

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