Skip to content

Instantly share code, notes, and snippets.

@CharlesRajendran
Last active October 1, 2019 02:02
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/71ff7ce895cf9fca21fb4b81883066ce to your computer and use it in GitHub Desktop.
Save CharlesRajendran/71ff7ce895cf9fca21fb4b81883066ce to your computer and use it in GitHub Desktop.
Generating Questions with Recursion
// generating questions of some complexity for kids math
// ["5 + 3 =", "3 + 2 =", "0 + 3 =", "2 + 0 =", "1 + 2 ="]
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);
@CharlesRajendran
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment