第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

顯示來自 API 的隨機問題

顯示來自 API 的隨機問題

猛跑小豬 2022-07-08 17:43:03
我有一個問題。我正在做一些測驗,我需要你的幫助。我們需要顯示不重復(fù)的隨機問題。我不知道怎么做。我是 JavaScript 的初學(xué)者。下面是我的代碼。 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++;        });    }}看看if循環(huán)。非常感謝!
查看完整描述

2 回答

?
胡子哥哥

TA貢獻1825條經(jīng)驗 獲得超6個贊

從高層次的角度來看,我會做如下:

  1. 有 2 個問題列表(針對 2 個年齡組)。

  2. 隨機播放相應(yīng)的一組問題

  3. 從問題集中彈出最后一個元素以顯示用戶。

  4. 重復(fù)。

好的,假設(shè)您有兩個問題列表或數(shù)組:

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'];

現(xiàn)在讓我們編寫一個小函數(shù)來打亂一個數(shù)組:


const shuffleArray = arr => arr

  .map(a => [Math.random(), a])

  .sort((a, b) => a[0] - b[0])

  .map(a => a[1]);

現(xiàn)在一些偽代碼來顯示場景:


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));


查看完整回答
反對 回復(fù) 2022-07-08
?
月關(guān)寶盒

TA貢獻1772條經(jīng)驗 獲得超5個贊

該命令let order = [...Array(numberOfItems).keys()].map(x => ({val:x,rand:Math.random()})).sort((a,b) => a.rand - b.rand).map(x => x.val);將創(chuàng)建一個長度numberOfItems很長的列表,但項目隨機排序。(項目將被 0 索引)。

例如,如果 numberOfItems = 10,這將給出一個類似的列表:[9, 5, 4, 1, 8, 3, 7, 6, 0, 2]。

然后,您可以簡單地瀏覽此列表,以確定要問哪個問題而無需重復(fù)。


查看完整回答
反對 回復(fù) 2022-07-08
  • 2 回答
  • 0 關(guān)注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號