Skip to content

Instantly share code, notes, and snippets.

@CharlesRajendran
Created October 1, 2019 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlesRajendran/b3720af263c26b24cb51e465e8df6666 to your computer and use it in GitHub Desktop.
Save CharlesRajendran/b3720af263c26b24cb51e465e8df6666 to your computer and use it in GitHub Desktop.
Generating Questions with Recursion
// generating questions of some complexity for kids math
// ["8 + 8 =", "8 + 5 =", "6 + 5 =", "3 + 9 =", "4 + 8 ="]
function generateRandomInteger(complexity) {
return Math.floor(Math.random() * complexity);
}
function matchingPairs(complexity) {
const x = generateRandomInteger(complexity);
const y = generateRandomInteger(complexity);
if ((x + y) >= complexity) {
return [x, y];
} else {
return matchingPairs(complexity);
}
}
function randomQuestionGenerator(n, complexity) {
let questions = [];
for(let i = 0; i < n; i++) {
const [x, y] = matchingPairs(complexity);
questions.push(`${x} + ${y} =`);
};
return questions;
}
const ques = randomQuestionGenerator(5, 10);
console.log(ques);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment