Created
October 1, 2019 01:56
-
-
Save CharlesRajendran/b3720af263c26b24cb51e465e8df6666 to your computer and use it in GitHub Desktop.
Generating Questions with Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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