I have been attempting to convert the JavaScript function into AngularJS, but I keep encountering errors due to my limited experience with AngularJS.
var array = [5, 5, 7, 9, 9, 9];
var max = array[0],
total = 0;
array.forEach((a) => {
if (a == max) {
total += max;
} else if (a > max) {
max = total = a;
}
});
console.log("total:", total);
The var array[]
is now being retrieved from GET data and I have made this adjustment in my AngularJS code.
$scope.List = getAllList;
$scope.deptime =function (){
$scope.array = $scope.List.Time;
console.log($scope.array);
$scope.max = $scope.array[0], $scope.total = 0;
angular.array.forEach((a)=>{
if(a==$scope.max){
$scope.total+=$scope.max;
}
else if(a>$scope.max){
$scope.max = $scope.total = a;
}
});
console.log("total:"+$scope.total);
};
$scope.deptime();
I am currently facing this error:
TypeError: Cannot read property '0' of undefined
Where could I possibly be making a mistake?
EDIT :- My response from the service is being obtained in this section :- $scope.List = getAllList;