npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

maths-problems

v1.1.2

Published

Randomly generate maths questions from a general problem template

Downloads

159

Readme

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