浏览代码

Fix percentage recalculation when both votes are zero

Aanand Prasad 9 年之前
父节点
当前提交
1f1a6f97a2
共有 1 个文件被更改,包括 22 次插入17 次删除
  1. 22 17
      result-app/views/app.js

+ 22 - 17
result-app/views/app.js

@@ -5,15 +5,6 @@ var bg1 = document.getElementById('background-stats-1');
 var bg2 = document.getElementById('background-stats-2');
 var bg2 = document.getElementById('background-stats-2');
 
 
 app.controller('statsCtrl', function($scope){
 app.controller('statsCtrl', function($scope){
-  var animateStats = function(a,b){
-    if(a+b>0){
-      var percentA = a/(a+b)*100;
-      var percentB = 100-percentA;
-      bg1.style.width= percentA+"%";
-      bg2.style.width = percentB+"%";
-    }
-  };
-
   $scope.aPercent = 50;
   $scope.aPercent = 50;
   $scope.bPercent = 50;
   $scope.bPercent = 50;
 
 
@@ -23,15 +14,16 @@ app.controller('statsCtrl', function($scope){
        var a = parseInt(data.a || 0);
        var a = parseInt(data.a || 0);
        var b = parseInt(data.b || 0);
        var b = parseInt(data.b || 0);
 
 
-       animateStats(a, b);
+       var percentages = getPercentages(a, b);
+
+       bg1.style.width = percentages.a + "%";
+       bg2.style.width = percentages.b + "%";
 
 
-       $scope.$apply(function() {
-         if(a + b > 0){
-           $scope.aPercent = a/(a+b) * 100;
-           $scope.bPercent = b/(a+b) * 100;
-           $scope.total = a + b
-         }
-      });
+       $scope.$apply(function () {
+         $scope.aPercent = percentages.a;
+         $scope.bPercent = percentages.b;
+         $scope.total = a + b;
+       });
     });
     });
   };
   };
 
 
@@ -43,3 +35,16 @@ app.controller('statsCtrl', function($scope){
     init();
     init();
   });
   });
 });
 });
+
+function getPercentages(a, b) {
+  var result = {};
+
+  if (a + b > 0) {
+    result.a = Math.round(a / (a + b) * 100);
+    result.b = 100 - result.a;
+  } else {
+    result.a = result.b = 50;
+  }
+
+  return result;
+}