将静态问题变成动态问题

问题描述 投票:0回答:4

我有如下静态问题。我在博客中使用这些问题进行测验。

var quiz = {
    "data": [
        {
            "question": "What is 10 + 5 ?",
            "answer": "15",
        },
        {
            "question": "What is 10 - 5 ?",
            "answer": "5",
        },
        {
            "question": "What is 10 + (- 5) ?",
            "answer": "5",
        },
        {
            "question": "What is -10 + 5 ?",
            "answer": "-5",
        },
        {
            "question": "What is -6 + (-6) ?",
            "answer": "-12",
        },
    ]
}

我如何使问题和答案动态化?换句话说,每个数字的取值范围是1到20。答案将根据问题进行计算。

感谢

javascript math random
4个回答
0
投票

写一个函数,它将给您两个随机数b / w -20:+ 20及其和。

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

function getRandomAndTheirSum(){
  var lower = getRandomInt(-20, 21)
  var upper = getRandomInt(-20, 21)
  return lower, upper, lower + upper
}

现在多次调用getRandomAndTheirSum并创建对象


0
投票

您可以使用template literalMath.random

// define all the airthmeatic operation

let operations = ["+","-","*","/"]

// map airthmeatic sign to related function
let funcs = {
  "+" : (a,b)=> a+b,
  "-" : (a,b)=> a-b,
  "*":  (a,b)=> a*b,
  "/" : (a,b)=> a/b
}

let i=2

// a loop to build a fix number of question
while(i--){
  // get random value in range of 20
  let a = Math.floor(Math.random() * 20)
  let b = Math.floor(Math.random() * 20)
  
  // select a random operation
  let operation =operations[Math.floor(Math.random() * (operations.length))]
  
  // call quizBuilder to build a random question
  randomQuizBuilder(a,b, operation)
}

let randomQuizBuilder = (a,b,operation) =>{
  let val = prompt(`what is ${a} ${operation} ${b}`) 
  console.log(+val === funcs[operation](a,b))
}

0
投票

function generateQuestion(template, min, max){
  // Generate first random number
  var numOne = Math.floor(Math.random() * (max - min) ) + min;
  // Generate second random number
  var numTwo = Math.floor(Math.random() * (max - min) ) + min;
  
  // append first question part
  var question = template[0];
  // check if first number is negative to put brackets around the string
  if(numOne < 0 )
    question += "(" + numOne + ")";
  else
    question +=  numOne;
   
  // append operator
  question += " + ";
  
  // check if second number is negative to put brackets around the string
   if(numTwo < 0 )
    question += "(" + numTwo + ")";
  else
    question +=  numTwo;
    
  // append last part of question
  question += template[1];
    
    
  // return your object
  return {
    question: question, 
    answer: numOne + numTwo
  };
}

var numberOfQuestions = 4;
var questions = [];
for(var i = 0; i < numberOfQuestions; i++)
  questions.push(generateQuestion(["What is ", "?"], -20, 20))
  
  
console.log(questions);

0
投票

我发现expr-eval包对于您的用例非常有用。

一个简化的示例:

const { Parser } = exprEval;

const randomNumber = (min, max) => Math.floor(Math.random() * max) + min;

const quiz = {
  equation: `${randomNumber(1, 20)} + ${randomNumber(1, 20)}`,

  get question() {
    return `What is ${this.equation.toString()}`;
  },

  get answer() {
    return `The answer is: ${Parser.evaluate(this.equation)} 👩‍🎓`;
  },
};

console.log(quiz.question);
console.log(quiz.answer)
<script src="https://cdnjs.cloudflare.com/ajax/libs/expr-eval/2.0.2/bundle.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.