Randomise Position of multiple Choices
# 🤝help
m
I am building a quiz bot with Bot press, with my questions, correct answer, and incorrect answers stored in a file and retrieved from their columns. The challenge I am facing is, the correct answer will always be in the same position - i.e A in this case. How can I randomise the positions for every run? so that the correct answer could be in any position - A, B, C, or D. After this, how should I go about the flow, as I would like to move to a specific node if the correct answer is selected.
@magnificent-vegetable-65154 Here's a simple solution as well, which I've heard works every time https://discord.com/channels/1108396290624213082/1176236272231141416 It uses this: "In expression card i typed "randomize order of items in @workflow.results" and it seems to work as intended" I always want to do everything as far as possible with normal code, and not use AI for everything at the beginning of a project. My approach is, if something can be done with 10 lines of code and then it always works 100%, that's better than using AI, which almost always follows our instructions (if it feels like it). Also, if something comes up in regular code that I don't understand, I ask the AI (GPT-4) to explain it to me, or if I've been banging my head against a wall with a problem for several hours, I can ask the AI for an alternative solution. Everyone can build 10 times faster with AI nowadays, but still, if your goal is to learn some useful coding for AI projects also, I (as a programmer) wouldn't miss the chance to try to solve the problem by coding first. What a pain, and in the end, what a pleasure!
Try if something like this works for you, otherwise we'll continue 👍 Code to randomize the answers in Single Choice
Copy code
js
const shuffleArray = (array) => {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
};

const rightAnswer = 4;
const wrongAnswers = [3, 5, 6];

const allAnswers = [rightAnswer, ...wrongAnswers];
const randomOrder = shuffleArray(allAnswers);

workflow.randomOrder = randomOrder;
workflow.rightAnswer = rightAnswer;
and after that two expression cards to check if the user's answer was correct or not, and based on that continue to the next Node.
m
Dear @quick-musician-29561 thank you very much for the response and sharing these. I will try it out and revert soon.
Hello again @quick-musician-29561 just to confirm your solution worked. Thank you very much! Greatly appreciate the help! 🫡
q
That's great to hear! 💎 🤝
6 Views