创建随机问答 [英] Creating random questions and answers javascript

查看:31
本文介绍了创建随机问答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站的问答"部分,向用户询问了40个可能的问题中的6个问题.每个答案都有设定的答案,但是没有正确的答案.如何在Javascript中将其随机化,以便不会总是问相同的问题?我知道可以用一个数组来解决问题和答案,但是不知道如何将它们随机分配给每一页.这是我的html代码:

I have a question and answer section of my site, where the user is asked 6 questions of out 40 possible questions. There are set answers for each one, but there isn't a correct answer. How do I randomize this in Javascript so that the same questions aren't always asked? I know it can be done with an array for the question and answers but don't know how to randomize them then for each page. This is my html code:

    <h2>What is the weather like today?</h2>

    <div class="answers">

    <div class="answers-left">
        <div class="answer1" tabIndex="1">Sunny</div>
        <div class="answer2" tabIndex="2">Raining</div>
    </div>
    <div class="answers-right">
        <div class="answer3" tabIndex="3">Cloudy</div>
        <div class="answer4" tabIndex="4">Windy</div>
    </div>

        <div class="clear"></div>
        </div>

<div class="next-button">
    <a class="ui-btn" href="question-2.html" rel="external">Next</a>
        </div>

如您所见,用户将选择一个答案,然后单击下一步"转到下一个问题.我只想一次显示一个问题.任何帮助将不胜感激!

As you can see, the user will select an answer and then click next to go to the next question. I only want one question to show at a time. Any help will be greatly appreciated!

推荐答案

步骤1)声明问题表:

var questions = [1, 2, 3, 4, 5, ..., 40];

第2步)获得第一个随机问题

Step 2) Get the 1st random question

var random1 = Math.floor(Math.random() * questions.length) ;
var choice1 = questions[random1];

步骤3)将其从表格中删除

Step 3) Remove it from the table

questions.splice(random1, 1);

重复步骤2和3提出更多问题

Repeat steps 2 & 3 to get more questions

这篇关于创建随机问答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆