Socket
Socket
Sign inDemoInstall

maths-problems

Package Overview
Dependencies
5
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    maths-problems

Randomly generate maths questions from a general problem template


Version published
Weekly downloads
5
increased by400%
Maintainers
1
Install size
5.64 MB
Created
Weekly downloads
 

Readme

Source

maths-problems.js

maths-problems allows you to generate random maths questions with answers that can be validated.

See examples/addition-example.js for a basic example usage.

Methods

generateQuestions ([problems], [numbers])

[problems] => an array of problems to use to generate questions.

[numbers] => an array of integers defining how many questions to generate for each problem.

Returns [questions] an array of generated questions.

Example:

var problems = require('maths-problems');

var additionProblem = {
  "question" : "What is {x=randomInt(1,10)} + {y=randomInt(1,10)}?",
  "answer" : ["{x}+{y}"],
  "answerFormat" : "0"
};
var subtractionProblem = {
  "question" : "What is {x=randomInt(1,10)} - {y=randomInt(1,10)}?",
  "answer" : ["{x}-{y}"],
  "answerFormat" : "0"
};

// Generate 5 addition questions and 5 subtraction questions
var questions = problems.generateQuestions([additionProblem, subtractionProblem], [5,5]);

markQuestion (question, problem)

question => the question object with an extra field - an array called "userAnswer" containing the user's answer for each part of the question problem => the problem used to generate the question

Returns a [result] object with a boolean property "correct" and an array "answer" containing the correct answers.

Example:

var questions = problems.generateQuestions([additionProblem, subtractionProblem], [5,5]);
// Continuing from previous generate example

... obtain an answer from the user and store it in question.userAnswer[] ...

// For example marking an additionProblem
var result = problems.markQuestion(question, additionProblem);
if (result.correct) {
  console.log("Correct!");
} else {
  console.log("Incorrect! Correct answer: " + result.answer[0]);
}

Problems

problemSchema = {
  "question" : "What is {x=randomInt(1,10)} + {y=randomInt(1,10)}?",
  "answer" : ["{x}+{y}"],
  "answerFormat" : "0"
}

question = The question text that will appear to the student after replacing variables (declared within curly brackets {}) with numeric values returned by the mathjs function.

answer = The answer field is an array of strings representing formulas to calculate the answer to each part of the question. Variables can be referenced by their name within curly brackets ({}).

answerFormat = Format the answer should be written in where each integer refers to an index in the answers array. For example, "0" => the value of "{x}+{y}" after evaluated. We can use this to add units or algebraic variables eg. "0 cm" or "0x".

Questions

Problems are used to generate questions, an example Question object:

questionSchema = {
    "text" : "What is 5 + 4?",
    "variables" :
      [{
        "name":"x",
        "value": 5
      },
      {
        "name":"y",
        "value":4
      }]
}

text = The actual question text to show to the student. A Problem's question with variable values added.

variables = An array of variables each with a name and value. In this case the value is the output of the mathjs function randomInt(1,10).

FAQs

Last updated on 12 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc