From 6d528775a8db73ec3cd987ec5c0b443298c1490d Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 28 Jan 2020 12:11:00 +0100 Subject: [PATCH] Fix some regressions where text questions were falsely identified as invalid. --- src/main.js | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/main.js b/src/main.js index 217492b..68f16c4 100644 --- a/src/main.js +++ b/src/main.js @@ -185,21 +185,22 @@ function parseLine(line) { return null } - // For multiple-choice, split lines into array - if (q.type == 'ChooseOne' || q.type == 'ChooseAny') { - if (els[2] || els[2] != '') { - q.choices = els[2].split('
') - } else { - alert('"Choose" question but no choices given at question "' - + q.question + '"') - return null - } + // Split choices into array + // Choices for text questions are labels + // No choices (empty string) are okay for text questions + // Also filter out empty lines because some people can't use spreadsheets + if (els[2] || (q.type == 'Text' && els[2] == '')) { + q.choices = els[2].split('
').filter(el => el != "") + } else { + alert('No choices given at question "' + + q.question + '"') + return null } // If multiple answers are allowed, they are newline-separated // Also filter out empty lines because some people can't use spreadsheets - if (els[3] || els[3] != '') { - q.answers = els[3].split('
').filter(el => el != ""); + if (els[3] && els[3] != '') { + q.answers = els[3].split('
').filter(el => el != "") } else { alert('No answers given at question "' + q.question + '"') @@ -232,12 +233,12 @@ function parseSpreadsheet() { * 6. Re-add outer tabs (ab) */ - var text = textEl.value.replace(/\t"([^"\n]|"")+\n([^"]|"")*"\t/g, + var text = textEl.value.replace(/\t"([^"\n]|"")+\n([^"]|"")*"/g, m => m.replace(/\n/g, '⎊') .replace(/""/g, '"') - .replace(/^\t"(.*)"\t$/, '$1') + .replace(/^\t"(.*)"$/, '$1') .replace(/\t/g, ' ') - .replace(/^(.*)$/g, '\t$1\t') + .replace(/^(.*)$/g, '\t$1') ) state.questions = text.split('\n').map(parseLine) @@ -280,10 +281,11 @@ function renderQuiz() { ? `
` : `
` - } else { + } else if (q.type == 'Text') { - for (c of q.choices) - html += `` + // If choices were given, use them as labels, otherwise use generic label + for ([ai, a] of q.answers.entries()) + html += `` }