Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- const surveyRegex = /<svg>((.|\n)*?)<\/svg>/g;
- const format = /<cat><text>((.|\n)*)\[((.|\n)*)]((.|\n)*)<\/text><\/cat>\s*<cat>((.|\n)*)<\/cat>/g;
- const valuesRegex = /\s*<g><val>([0-9]+)<\/val>([0-9]+)<\/g>/g;
- let matches = surveyRegex.exec(args);
- const validMatches = [];
- let survey = '';
- let values = '';
- if (matches) {
- while (matches) {
- const match = matches[1];
- const isValid = format.exec(match);
- if (isValid) {
- validMatches.push(match);
- survey = isValid[3];
- values = isValid[7];
- }
- matches = surveyRegex.exec(args);
- }
- if (validMatches.length > 0) {
- let sum = 0;
- let votes = 0;
- let valuesMatches = valuesRegex.exec(values);
- while (valuesMatches) {
- const value = Number(valuesMatches[1]);
- const count = Number(valuesMatches[2]);
- if (value > 0 && value <= 10) {
- sum += value * count;
- votes += count;
- valuesMatches = valuesRegex.exec(values);
- }
- }
- let average = parseFloat((sum / votes).toFixed(2));
- if (votes === 0) {
- average = 0;
- }
- console.log(`${survey}: ${average}`);
- } else {
- console.log('Invalid format');
- }
- } else {
- console.log('No survey found');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement