JSFiddle - React, Tailwind, and code Playground
by Josh Pullen
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js"></script>
JavaScript
const assignment = {
name: "",
dueDate: new Date(),
specialDueDates: [
{ user: "PullJosh", date: new Date() }
],
content: [
{ type: "text", value: "Here are the instructions for your assignment. First, do one thing. Then, do another." },
{
type: "question",
questionType: "addition",
data: {
a: 1,
b: 1
}
},
{
type: "challenge",
id: "challenge1",
requiredProgress: 1.00
}
]
}
function getQuestionData(seed, progress) {
Math.seedrandom(seed)
if (progress <= 0.5) {
return {
a: Math.floor(Math.random() * 10),
b: Math.floor(Math.random() * 10)
}
} else {
return {
a: Math.floor(Math.random() * 10) + 5,
b: Math.floor(Math.random() * 10) + 5
}
}
}
function checkAnswer({ sum }, { a, b }) {
return sum === a + b
}
function* generateQuestions(username, attempt) {
const seedBase = `${username}-${attempt}`
let questionNumber = 1
let progress = 0.0
while (progress < 1.0) {
const seed = `${seedBase}-${questionNumber}`
const questionData = getQuestionData(seed, progress)
const answer = yield { progress, questionData }
if (checkAnswer(answer, questionData)) {
progress = Math.min(1, progress + 0.2)
} else {
progress = Math.max(0, progress - 0.2)
}
questionNumber++
}
return { progress }
}
const gen = generateQuestions("PullJosh", 1)
let state = gen.next()
while (!state.done) {
const { a, b } = state.value.questionData
const answer = prompt(`What is ${a} + ${b}?`)
if (answer === null) break;
state = gen.next({ sum: Number(answer) })
console.log(state)
}