Advertisement
Guest User

Untitled

a guest
Feb 11th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function task3(inp){
  2.     if (inp.indexOf(`<svg>`) == -1 || inp.indexOf(`</svg>`) == -1){
  3.         return "No survey found"
  4.     }
  5.     inp = inp.substring(inp.indexOf(`<svg>`) + 5);
  6.     inp = inp.substring(0, inp.indexOf(`</svg>`));
  7.    
  8.     let Cat1 = /<cat>[ ]*<text>(.|\n)*\[(.|\n)*\](.|\n)*<\/text>[ ]*<\/cat>/g;
  9.     let Cat2 = /<cat>[ ]*((.|\n)*[ ]*<g>[ ]*<val>[ ]*([1-9]|10)[ ]*<\/val>[ ]*[0-9]+[ ]*<\/g>(.|\n)*)+<\/cat>/g;
  10.     let values = /<g>[ ]*<val>[ ]*([1-9]|10)[ ]*<\/val>[ ]*[0-9]+[ ]*<\/g>/g;
  11.    
  12.     let Cat1Text, Cat2Text;
  13.     try{
  14.         Cat2Text = inp.match(Cat2)[0];
  15.         Cat1Text = inp.match(Cat1)[0];
  16.     }catch (err){
  17.         return "Invalid format"
  18.     }
  19.    
  20.     Cat1Text = Cat1Text.substring(Cat1Text.indexOf('[') + 1);
  21.     Cat1Text = Cat1Text.substring(0, Cat1Text.indexOf(']'));
  22.    
  23.     Cat2Values = Cat2Text.match(values);
  24.    
  25.     if (Cat2Values.length <= 0){
  26.         return "Invalid format"
  27.     }
  28.    
  29.     let questionValues = [];
  30.     let questionCounts = [];
  31.     for (let i = 0; i < Cat2Values.length; i++){
  32.         let item = Cat2Values[i] // used for answer value
  33.         item = item.substring(item.indexOf("<val>") + 5);
  34.         item = item.substring(0, item.indexOf("</val>")).trim();
  35.         questionValues[i] = parseInt(item);
  36.         let item2 = Cat2Values[i] // used for value count
  37.         item2 = item2.substring(item2.indexOf("</val>") + 6);
  38.         item2 = item2.substring(0, item2.indexOf("</g>")).trim();
  39.         questionCounts[i] = parseInt(item2)
  40.        
  41.     }  
  42.    
  43.     let scoreSum = 0;
  44.     let totalScores = 0;
  45.     for(let i = 0; i < questionValues.length; i++){
  46.         scoreSum += questionValues[i] * questionCounts[i];
  47.         totalScores += questionCounts[i];
  48.     }
  49.    
  50.     let output = `${Cat1Text}: ${parseFloat((scoreSum / totalScores).toFixed(2))}`;
  51.    
  52.     return output;
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement