-2

I am trying to convert this JavaScript function to AngularJS, but getting few errors as I am new in 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 coming from GET data and I did this 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();

So I am facing this error:

TypeError: Cannot read property '0' of undefined

Where am I going wrong?

EDIT :- I am getting my response from the service in this part :- $scope.List = getAllList;

georgeawg
  • 48,608
  • 13
  • 72
  • 95
WhoAmI
  • 217
  • 1
  • 2
  • 23
  • Replace `angular.array` with `$scope.array` – Saeed May 21 '18 at 15:02
  • @Saeed.Ataee Getting same error. – WhoAmI May 21 '18 at 15:03
  • 1
    Log `$scope.List.Time;` and insert a sample – Saeed May 21 '18 at 15:04
  • 2
    Show more of your code, specifically the part where you fetch `$scope.List.Time`. High probability that this is a duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/502381) – JJJ May 21 '18 at 15:06
  • There's no reason to change anything in your existing script. Putting `$scope` in front of all the variables isn't necessarily correct and depends on whether you want those variables in the module's scope. Your existing code will work just fine and doesn't need to be "converted" at all. – Reinstate Monica Cellio May 21 '18 at 15:06
  • using `angular.array` is true here instead of `$scope.array`? @Archer – Saeed May 21 '18 at 15:08
  • No - there's nothing wrong with your original code - `array.forEach()`. Just take your original code and wrap it in `$scope.deptime = function ()`. BUT do pay attention to the other comments. If `$scope.List.Time` is not populated correctly when the function is executed then it won't work. – Reinstate Monica Cellio May 21 '18 at 15:09
  • @JJJ I don't think so. It's only functional details I am using here , rest is perfect – WhoAmI May 21 '18 at 15:10
  • 1
    The error message means that `$scope.List.Time` doesn't exist. If you want us to tell why it doesn't exist, you have to show a [mcve]. Otherwise you'll have to debug the code by yourself. – JJJ May 21 '18 at 15:11
  • @Archer Yes wrapping will do perfect I know . But now when I am converting my script to angular javascript I am facing issue,like my array is now from service GET call. – WhoAmI May 21 '18 at 15:13
  • Okay, then in that case @JJJ is most likely correct and your problem is that you are trying to use `$scope.List.Time` *before it is populated by an asynchronous call*. Look at the comment from Saeed.Ataee where he suggested you log that variable and check it's populated. (The error says it isn't populated). – Reinstate Monica Cellio May 21 '18 at 15:15
  • @Archer Ok . I am looking for it. – WhoAmI May 21 '18 at 15:16

1 Answers1

1

If getAllList service implemented correct, change your code to this

getAllList()
  .then(function(res) {
    $scope.List = res;
    $scope.deptime();
  });

also change angular.array to $scope.array in deptime

$scope.array.forEach((a) => {
  if (a == $scope.max) {
    $scope.total += $scope.max;
  } else if (a > $scope.max) {
    $scope.max = $scope.total = a;
  }
});

Also, if you don't need max, total and array outside of function, change your function to this

$scope.deptime = function() {
  let array = $scope.List.Time;
  let max = $scope.array[0],
    total = 0;
  $scope.array.forEach((a) => {
    if (a == max) {
      total += max;
    } else if (a > max) {
      max = total = a;
    }
  });
  console.log("total:" + total);
  return total;
};
Saeed
  • 5,413
  • 3
  • 26
  • 40