Skip to content

Commit

Permalink
randomize multiple choices option
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasDuelli committed Jan 24, 2022
1 parent fbc867e commit 40a00e0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/FeedbackForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ export const buildForm = function (feedbackOptions, overrideLanguage) {
};

var optionHTML = "";

if (formItem.randomizeChoices) {
formItem.choices = shuffle(formItem.choices);
}

for (var j = 0; j < formItem.choices.length; j++) {
optionHTML += getOptionHTML(formItem, formItem.choices[j]);
}
Expand Down Expand Up @@ -309,6 +314,23 @@ export const buildForm = function (feedbackOptions, overrideLanguage) {
return formHTML;
};

function shuffle(array) {
let currentIndex = array.length,
randomIndex;

while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;

[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}

return array;
}

export const getFormData = function (form) {
var formData = {};
for (var i = 0; i < form.length; i++) {
Expand Down

0 comments on commit 40a00e0

Please sign in to comment.