26 lines
1018 B
JavaScript
26 lines
1018 B
JavaScript
const extractAnswerFromQuestion = (previous, question) => {
|
|
let answer = question.correctAnswer;
|
|
if (question.getType() === 'matrix') {
|
|
const correctAnswer = question.correctAnswer;
|
|
const questionRows = question.getRows();
|
|
const keys = questionRows.map(question => {
|
|
const text = question.value;
|
|
if (/[,.!?]/.test(text.slice(-1))) {
|
|
return text.slice(0, -1);
|
|
}
|
|
return text;
|
|
}); // get the keys as they appear in the question, without punctuation at the end
|
|
|
|
answer = keys.map(key => `${key}: ${correctAnswer[key]}`); // return an array, it gets converted to a string further up
|
|
}
|
|
return [...previous, {title: question.title, answer, type: question.getType()}];
|
|
};
|
|
|
|
export const extractSurveySolutions = (prev, element) => {
|
|
if (!element || !element.elements) {
|
|
// element does not exist or does not have children, so just return the previous result
|
|
return prev;
|
|
}
|
|
return [...prev, ...element.elements.reduce(extractAnswerFromQuestion, [])];
|
|
};
|