javascript - average function not returning correct output after editing values -


i new angularjs, , have slight problem. have list of cities compute average temperature, , reason, after editing temperature values in list of cities, function compute average temperature returning incorrect values. returns correct value after loading page, after editing temperature values incorrect results.

here code:

<html ng-app> <head>     <title>weather comparison</title> </head> <body ng-controller='weathercontroller'> <h1>weather comparison</h1> <div ng-repeat='item in items'>     <span>{{item.city}}</span>     <input ng-model='item.temp'>     <input ng-model='item.humidity'>     <input ng-model='item.wind'>     <input ng-model='item.cloudiness'> </div> <div>the average temperature {{avgtemp}}</div> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> <script>     function weathercontroller($scope) {     $scope.items = [         {city: 'phoenix,az', temp: 38, humidity: 10, wind: 3, cloudiness: 10},         {city: 'atlanta,ga', temp: 30, humidity: 80, wind: 7, cloudiness: 40},         {city: 'honolulu,hi', temp: 32, humidity: 75, wind: 8, cloudiness: 30}     ];      //returns incorrect average values after editing default temperature values     $scope.$watch(function() {         var sum = 0;         (var = 0; < $scope.items.length; i++) {             sum += $scope.items[i].temp;         }         $scope.avgtemp = sum / $scope.items.length;    }); }  </script> </body> </html> 

does know doing wrong here?

the temperature string , need convert integer (demo):

sum += parseint($scope.items[i].temp, 10); 

or if want use floats:

sum += parsefloat($scope.items[i].temp); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -