Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function task3(inp){
- if (inp.indexOf(`<svg>`) == -1 || inp.indexOf(`</svg>`) == -1){
- return "No survey found"
- }
- inp = inp.substring(inp.indexOf(`<svg>`) + 5);
- inp = inp.substring(0, inp.indexOf(`</svg>`));
- let Cat1 = /<cat>[ ]*<text>(.|\n)*\[(.|\n)*\](.|\n)*<\/text>[ ]*<\/cat>/g;
- let Cat2 = /<cat>[ ]*((.|\n)*[ ]*<g>[ ]*<val>[ ]*([1-9]|10)[ ]*<\/val>[ ]*[0-9]+[ ]*<\/g>(.|\n)*)+<\/cat>/g;
- let values = /<g>[ ]*<val>[ ]*([1-9]|10)[ ]*<\/val>[ ]*[0-9]+[ ]*<\/g>/g;
- let Cat1Text, Cat2Text;
- try{
- Cat2Text = inp.match(Cat2)[0];
- Cat1Text = inp.match(Cat1)[0];
- }catch (err){
- return "Invalid format"
- }
- Cat1Text = Cat1Text.substring(Cat1Text.indexOf('[') + 1);
- Cat1Text = Cat1Text.substring(0, Cat1Text.indexOf(']'));
- Cat2Values = Cat2Text.match(values);
- if (Cat2Values.length <= 0){
- return "Invalid format"
- }
- let questionValues = [];
- let questionCounts = [];
- for (let i = 0; i < Cat2Values.length; i++){
- let item = Cat2Values[i] // used for answer value
- item = item.substring(item.indexOf("<val>") + 5);
- item = item.substring(0, item.indexOf("</val>")).trim();
- questionValues[i] = parseInt(item);
- let item2 = Cat2Values[i] // used for value count
- item2 = item2.substring(item2.indexOf("</val>") + 6);
- item2 = item2.substring(0, item2.indexOf("</g>")).trim();
- questionCounts[i] = parseInt(item2)
- }
- let scoreSum = 0;
- let totalScores = 0;
- for(let i = 0; i < questionValues.length; i++){
- scoreSum += questionValues[i] * questionCounts[i];
- totalScores += questionCounts[i];
- }
- let output = `${Cat1Text}: ${parseFloat((scoreSum / totalScores).toFixed(2))}`;
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement