【问题标题】:How to loop through an array, randomly output two strings, and execute them against the answer?如何遍历一个数组,随机输出两个字符串,并针对答案执行它们?
【发布时间】:2015-02-11 09:23:47
【问题描述】:

我有四个组,每个组包含三个键:questionhintanswer。我只需要随机输出 questionhint 两个值,然后针对 answer 值执行它们,用户必须在其中输入文本域。我的想法是遍历整个数组,然后随机输出,最后执行。

这怎么可能? http://jsfiddle.net/vo01u94p/

jQuery/JS

$('.submit').on('click', function () {
    var HISTORY;
    var question_1 = {
        question: 'When did Martin Luther King, Jr. die?',
        hint: 'He was born on January 15, 1929 and he died at the age of 39.',
        answer: 'April 4, 1968',
    };
    var question_2 = {
        question: 'Who discovered America?',
        hint: 'This person led three ships - the Nina, the Pinta and the Santa Maria - out of the Spanish port of Palos on August 3, 1492.',
        answer: 'Christopher Columbus',
    };
    var question_3 = {
        question: 'What event occured on July 4, 1776?',
        hint: 'Thomas Jefferson played an important role.',
        answer: 'The United States Declaration of Independence was written',
    };
    var question_4 = {
        question: "What continent covers 8.3% of the Earth's total surface area (28.4% of its land area)?",
        hint: 'Also known as the New World',
        answer: 'The Americas, or America',
    };
    var questions = [];
    questions[0] = question_1;
    questions[1] = question_2;
    questions[2] = question_3;
    questions[3] = question_4;
    var library = {
        questions: questions
    }
    var libraries = [];
    libraries[HISTORY] = library;
    var questionNo = 3;
    console.log(libraries[HISTORY].questions[questionNo].question);
    console.log(libraries[HISTORY].questions[questionNo].hint);
    console.log(libraries[HISTORY].questions[questionNo].answer);
});

【问题讨论】:

    标签: javascript jquery arrays random


    【解决方案1】:

    我会这样:

    使用变量r = Math.floor(Math.random()*(questions.length)); 在您的列表中获取一个随机问题,然后选择该问题,如果答案不正确,则显示提示,如果答案正确,splice() 您数组中的最后一个问题.

    var question_1 = {
      question: 'When did Martin Luther King, Jr. die?',
      hint: 'He was born on January 15, 1929 and he died at the age of 39.',
      answer: 'April 4, 1968'
    };
    var question_2 = {
      question: 'Who discovered America?',
      hint: 'This person led three ships - the Nina, the Pinta and the Santa Maria - out of the Spanish port of Palos on August 3, 1492.',
      answer: 'Christopher Columbus'
    };
    var question_3 = {
      question: 'What event occured on July 4, 1776?',
      hint: 'Thomas Jefferson played an important role.',
      answer: 'The United States Declaration of Independence was written'
    };
    var question_4 = {
      question: "What continent covers 8.3% of the Earth's total surface area (28.4% of its land area)?",
      hint: 'Also known as the New World',
      answer: 'The Americas, or America'
    };
    
    
    var questions = [];
    questions[0] = question_1;
    questions[1] = question_2;
    questions[2] = question_3;
    questions[3] = question_4;
    
    function newQuestion() {
      var r = Math.floor(Math.random() * (questions.length));
      $('#question').html(questions[r].question);
      $('button').unbind();
      $('button').click(function() {
        ($('#usr_answer').val() == questions[r].answer) ? goodAnswer(r): badAnswer(r);
      });
    }
    
    function goodAnswer(r) {
      $('#response').html('that was a god answer');
      $('#question').html('');
      $('#hint').html('');
      questions.splice(r, 1);
      (questions.length == 0) ? $('#response').html('No More questions'): newQuestion();
    }
    
    function badAnswer(r) {
      $('#hint').html(questions[r].hint);
      $('#response').html('Please try again');
    }
    $(newQuestion());
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p id="question"></p>
    <p id="hint"></p>
    <p>
      <input type="text" id="usr_answer" />
    </p>
    <button type="button" style="padding: 4px 22px; margin: 10px 0px 0px 0px;">OK</button>
    <div id="response"></div>

    【讨论】:

    • 是的,这就是我一开始所寻找的。非常感谢!
    【解决方案2】:

    试试这个,使用 for 循环

    查看下面的输出

        $('.submit').click( function(){
    		
    		var HISTORY;
    		
    		var question_1 = {
    		   question: 'When did Martin Luther King, Jr. die?',
    		   hint: 'He was born on January 15, 1929 and he died at the age of 39.',
    		   answer:'April 4, 1968',
    		   };
    		var question_2 = {
    		   question: 'Who discovered America?',
    		   hint: 'This person led three ships - the Nina, the Pinta and the Santa Maria - out of the Spanish port of Palos on August 3, 1492.',
    		   answer:'Christopher Columbus',
    		   };
    		var question_3 = {
    		   question: 'What event occured on July 4, 1776?',
    		   hint: 'Thomas Jefferson played an important role.',
    		   answer:'The United States Declaration of Independence was written',
    		   };
    		var question_4 = {
    		   question: "What continent covers 8.3% of the Earth's total surface area (28.4% of its land area)?",
    		   hint: 'Also known as the New World',
    		   answer:'The Americas, or America',
    		   };
    			  
    		
    		var questions = [];    
    		questions[0] = question_1;
    		questions[1] = question_2;
    		questions[2] = question_3;
    		questions[3] = question_4;
    		
    		
    		var library = { questions : questions }
    		var libraries = [];
    		libraries[HISTORY] = library;
    		var questionNo = 3;
    	
            for(a=0;a<libraries[HISTORY].questions.length;a++){ $("body").append("<br/>"+libraries[HISTORY].questions[a].question+"<br/>");
                                                              }
    		  
    		
    
    		
    		
    		
    		
    			});
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p><input type="text" /></p>
    <button class='submit' type="button" style="padding: 4px 22px; margin: 10px 0px 0px 0px;">OK</button>

    【讨论】:

    • 感谢您的回答!
    • 我很高兴@jQueryster :)
    【解决方案3】:

    您可以使用$.each()。这将让您遍历一个数组并让您访问索引和值。 JQuery 文档中有一些示例可供您参考。

    $.each(libraries, function(key, library) {
     $.each(library.questions, function(key, question) {
        console.log("Question: " + question.question);
        console.log("Hint: " + question.hint);
        console.log("Answer: " + question.answer);
     });
    });
    

    【讨论】:

      【解决方案4】:

      您可以使用一个变量来生成一个十进制的随机值并设置十进制值的范围。

      使用具有两次迭代的 for 循环。 示例:如果 rand 值在 0.0 到 0.25 的范围内,请询问问题编号。 1、

      .26 到 0.50 提出第 2 个问题,依此类推

      一旦第一次迭代即将结束,从数组列表中删除该问题索引并 执行第二次迭代。

      这样你可以随机生成两个问题。

      【讨论】:

      • 是的,这对我提出的问题很有帮助,但事实是我有超过 200 个问题。
      • 哦。那么@kaiido 的回答最适合
      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 2022-08-22
      • 2015-07-02
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多