From 40a00e018430ac312d9870eaad563a619e0c31bf Mon Sep 17 00:00:00 2001 From: Tobias Duelli Date: Mon, 24 Jan 2022 16:43:37 +0100 Subject: [PATCH] randomize multiple choices option --- src/FeedbackForm.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/FeedbackForm.js b/src/FeedbackForm.js index 94fc650a..d9aeefe2 100644 --- a/src/FeedbackForm.js +++ b/src/FeedbackForm.js @@ -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]); } @@ -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++) {