Skip to content

Commit 40a00e0

Browse files
committed
randomize multiple choices option
1 parent fbc867e commit 40a00e0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/FeedbackForm.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ export const buildForm = function (feedbackOptions, overrideLanguage) {
277277
};
278278

279279
var optionHTML = "";
280+
281+
if (formItem.randomizeChoices) {
282+
formItem.choices = shuffle(formItem.choices);
283+
}
284+
280285
for (var j = 0; j < formItem.choices.length; j++) {
281286
optionHTML += getOptionHTML(formItem, formItem.choices[j]);
282287
}
@@ -309,6 +314,23 @@ export const buildForm = function (feedbackOptions, overrideLanguage) {
309314
return formHTML;
310315
};
311316

317+
function shuffle(array) {
318+
let currentIndex = array.length,
319+
randomIndex;
320+
321+
while (currentIndex != 0) {
322+
randomIndex = Math.floor(Math.random() * currentIndex);
323+
currentIndex--;
324+
325+
[array[currentIndex], array[randomIndex]] = [
326+
array[randomIndex],
327+
array[currentIndex],
328+
];
329+
}
330+
331+
return array;
332+
}
333+
312334
export const getFormData = function (form) {
313335
var formData = {};
314336
for (var i = 0; i < form.length; i++) {

0 commit comments

Comments
 (0)