0

I have one question. I am making some quiz and I need your help. We need to display random questions with no repeat. I don't know how to make that. I'm a beginner in JavaScript. Under is my code.

 import { State } from "../../../framework/StateBase";
import { Conversation } from "../../../framework/ConversationBase";
import { getLanguage } from "../../../gateways/GigaaaGateway";
import { RatherGameGateway } from "../gateway/RatherGameGateway";

export class GetUserQuestionState extends State {
    constructor(conversation: Conversation, name: string) {
        super(conversation, name);
    }
    public configure(convo: any, bot: any) {
        convo.beforeThread(this.name, async convo => {

            let gateway = new RatherGameGateway();
            let age = this.conversation.payload.userAges;
            let ageCategory: number;

            try {
                let lang = await getLanguage(this.conversation.languageId, bot);
                if (age > 11) {
                    ageCategory = 0;
                    let result = await gateway.findLangCodeAndAge(lang.code, ageCategory);
                    if (this.conversation.payload.counter === 0) {
                        this.conversation.payload.params.question = result[0].questions[Math.floor(Math.random())];
                    } else if (this.conversation.payload.counter > 0) {
                        this.conversation.payload.params.next_question = result[0].questions[this.conversation.payload.counter];
                    }
                    this.conversation.payload.counter++;

                } else if (age >= 4 && age <= 11) {
                    ageCategory = 1;
                    let result = await gateway.findLangCodeAndAge(lang.code, ageCategory);
                    if (this.conversation.payload.counter === 0) {
                        this.conversation.payload.params.question = result[0].questions[0];
                    } else if (this.conversation.payload.counter > 0) {
                        this.conversation.payload.params.next_question = result[0].questions[this.conversation.payload.counter];
                    }
                    this.conversation.payload.counter++;
                }

            } catch (error) {
                this.conversation.payload.error = true;
            }
            convo.gotoThread(this.conversation.getNextState());
        });
    }
}

Just look if loop. Thank you very much!

  • Does this answer your question? [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Heretic Monkey Apr 21 '20 at 18:12

2 Answers2

0

The command let order = [...Array(numberOfItems).keys()].map(x => ({val:x,rand:Math.random()})).sort((a,b) => a.rand - b.rand).map(x => x.val); will create a list of length numberOfItems long, but with items randomly sorted. (Items will be 0 indexed).

For example if numberOfItems = 10, this will give a list like: [9, 5, 4, 1, 8, 3, 7, 6, 0, 2].

You can then simply run through this list in order to figure out which question to ask without repeating one.

bruceceng
  • 1,844
  • 18
  • 23
0

What I would do from a high level perspective is as follows:

  1. Have 2 lists of questions, (for the 2 age groups).
  2. Shuffle the corresponding set of questions
  3. pop the last element off the set of questions to show user.
  4. repeat.

Ok, so let's say you have two lists or arrays of questions:

let smallQuestions = ['how many fingers do you have', 'what does the dog say', 'what color is a sheep', 'how old are you'];
let bigQuestions = ['how many kids do you have', 'did you do your taxes', 'do you own a car', 'do you drink alcohol'];

Now let's write a little function to shuffle an array:

const shuffleArray = arr => arr
  .map(a => [Math.random(), a])
  .sort((a, b) => a[0] - b[0])
  .map(a => a[1]);

Now some pseudocode to show the scenario:

let smallQuestions = ['how many fingers do you have', 'what does the dog say', 'what color is a sheep', 'how old are you'];
let bigQuestions = ['how many kids do you have', 'did you do your taxes', 'do you own a car', 'do you drink alcohol'];
const shuffleArray = arr => arr
  .map(a => [Math.random(), a])
  .sort((a, b) => a[0] - b[0])
  .map(a => a[1]);

const askMeSomething = (age) => {
  if (age > 11) {
    bigQuestions = shuffleArray(bigQuestions);
    return bigQuestions.pop();
  } else if (age >= 4) {
    smallQuestions = shuffleArray(smallQuestions);
    return smallQuestions.pop() || 'out of questions';
  } else return 'too young';
}

console.log(askMeSomething(3));
console.log(askMeSomething(4));
console.log(askMeSomething(5));
console.log(askMeSomething(6));
console.log(askMeSomething(7));
console.log(askMeSomething(8));
Raz Chiriac
  • 406
  • 2
  • 7