A free & easy-to-use API for obtaining mathematical-related questions, categorised by topics such as algebra and arithmetic.
This API uses the following structure:
{
"arithmetic": {
"q1": {
"question": "What is 100/25?",
"options": ["3", "4", "5", "6", "7"],
"correctIndex": 1
}
},
"algebra": {
"q1": {
"question": "Find the value of X: '2x = 16'",
"options": ["4", "6", "8", "10", "12"],
"correctIndex": 2
}
}
...
}
All mathematical questions are categorised by their topics.
A simple XHTTPRequest in JavaScript for fetching and displaying a randomly selected question from the API:
const question = document.getElementById("question");
const answerChoices = document.getElementById("answerChoices");
let xhttp = new XMLHttpRequest();
/**
* Randomly selects a question fron the data that falls under the given 'topic' string.
*
* @param {Object} data The JSON data returned & parsed from the XHTTP Request.
* @param {String} topic The JSON-like data returned from the XHTTP Request.
*/
const chooseQuestion = (data, topic) => {
const maxIdx = Object.keys(data[topic]).length;
const index = Math.floor(Math.random() * maxIdx);
return data[topic][`q${index}`];
}
/**
* Adds a randomly selected question to the DOM.
*
* @param {String} responseTxt The JSON-like data returned from the XHTTP Request.
*/
const addQuestion = (responseTxt) => {
data = JSON.parse(responseTxt)
topic = "arithmetic"
q = chooseQuestion(data, topic)
console.log(q);
question.textContent = q["question"];
for (let i = 0; i < q["options"].length; i++){
const optionBtn = document.createElement("button");
optionBtn.textContent = q["options"][i];
answerChoices.appendChild(optionBtn);
}
}
xhttp.onreadystatechange = () => {
if (xhttp.readyState == 4 && xhttp.status == 200) {
addQuestion(xhttp.responseText);
}
};
accessKey = "ILoveMath";
xhttp.open("GET", `https://{link}/mathsapi?accesskey=${accessKey}`);
xhttp.send();
If you encounter any issues while using MathsQuestionsAPI in your projects, feel free to open an issue ticket right here on the repository, and I'll get back to you ASAP.
If you have any feedback for anything about MathsQuestionsAPI, there are many ways to submit it.
- Via Email - jessydavies@hotmail.com
- Via the Issues section of this repository.
Jessica Davies
You may use MathsQuestionsAPI under the terms and conditions on the Apache licence. See LICENCE for details.