JSFiddle - React, Tailwind, and code Playground
by sandro_paganotti
JavaScript
function getTotal(answers){
return Object.keys(answers).reduce(function(total, cat){
return total + Math.max.apply(window,
Object.keys(answers[cat]).map(function(question){
return answers[cat][question].points === 15 ? 0 : answers[cat][question].points;
})
);
},0);
}
var answers = {
cat1: {
q1: {
"question": "Why?",
"answer": "No",
"points": 6
},
q2: {
"question": "Why oh why?",
"answer": "50%",
"points": 15
},
q3: {
"question": "Is it true?",
"answer": "Maybe",
"points": 9
}
},
cat2: {
q1: {
"question": "Hello?",
"answer": "Hello",
"points": 0
},
q2: {
"question": "Why me?",
"answer": "Because",
"points": 9
},
q3: {
"question": "Is it true?",
"answer": "Maybe",
"points": 0
}
},
cat3: {
q1: {
"question": "why not?",
"answer": "I don't know",
"points": 15
},
q2: {
"question": "What?",
"answer": "That",
"points": 9
},
q3: {
"question": "Is it real?",
"answer": "Nope",
"points": 6
}
}
};
console.log(getTotal(answers));