'use strict';

/*global results*/
(function(results) {
var totalScore = 0;
var totalWeight = 0;

  Object.keys(results).forEach(function(result) {
    var categoryWeight = 0;
    var categoryScore = 0;
    if (results[result].adviceList) {
      Object.keys(results[result].adviceList).forEach(function(advice) {
        if (results[result].adviceList[advice].score >= 0) {
          totalWeight += results[result].adviceList[advice].weight;
          categoryWeight += results[result].adviceList[advice].weight;

          if (results[result].adviceList[advice].score  !== 0) {
            totalScore += results[result].adviceList[advice].score * results[result].adviceList[advice].weight;
            categoryScore += results[result].adviceList[advice].score * results[result].adviceList[advice].weight;
          }
        }
      });
    }
    if (categoryWeight>0) {results[result].score = Math.round(categoryScore / categoryWeight)}
  });

  results["score"] = Math.round(totalScore / totalWeight);
})(results);

@@ -48,16 +48,18 @@
 ults) %7B%0A
+  
 var tota
@@ -72,13 +72,13 @@
  = 0
-;%0Avar
+,%0A   
  tot
@@ -162,27 +162,24 @@
 gory
-Weight
+Score
  = 0
-;
+,
 %0A    
-var
+ 
  cat
@@ -187,27 +187,39 @@
 gory
-Score = 0;%0A    if (
+Weight = 0,%0A      adviceList = 
 resu
@@ -240,16 +240,37 @@
 viceList
+;%0A%0A    if (adviceList
 ) %7B%0A    
@@ -283,32 +283,16 @@
 ct.keys(
-results%5Bresult%5D.
 adviceLi
@@ -318,16 +318,20 @@
 n(advice
+Name
 ) %7B%0A    
@@ -338,120 +338,89 @@
     
-if (results%5Bresult%5D.adviceList%5Badvice%5D.score %3E= 0) %7B%0A          totalWeight += results%5Bresult%5D.adviceList%5B
+var advice = adviceList%5BadviceName%5D;%0A%0A        totalScore += advice.score * 
 advice
-%5D
 .wei
@@ -428,34 +428,32 @@
 ht;%0A        
- 
- 
 category
 Weight += re
@@ -444,52 +444,38 @@
 gory
-Weight += results%5Bresult%5D.adviceList%5B
+Score += advice.score * 
 advice
-%5D
 .wei
@@ -492,168 +492,29 @@
     
-  
-if (results%5Bresult%5D.adviceList%5Badvice%5D.score  !== 0) %7B%0A            totalScore += results%5Bresult%5D.adviceList%5Badvice%5D.score * results%5Bresult%5D.adviceList%5Badvice%5D
+totalWeight += advice
 .wei
@@ -514,28 +514,24 @@
 ice.weight;%0A
-    
         cate
@@ -538,124 +538,32 @@
 gory
-Score += results%5Bresult%5D.adviceList%5Badvice%5D.score * results%5Bresult%5D.adviceList%5Badvice%5D.weight;%0A          %7D%0A        %7D
+Weight += advice.weight;
 %0A   
@@ -601,13 +601,22 @@
 ight
-%3E
+ %3E 
 0) %7B
+%0A      
 resu
@@ -677,16 +677,21 @@
 yWeight)
+%0A    
 %7D%0A  %7D);%0A
@@ -704,17 +704,14 @@
 ults
-%5B%22
+.
 score
-%22%5D
  = M

Refactor slightly for readability.