xxxxxxxxxx
56
let question; //question text
let answer; //answer text
let num; //number to be shown
let isQuestion = true; //program state (question or answer)
//numbers to be squared
let minNum = 2;
let maxNum = 16;
let nums = [];
function setup() {
createCanvas(400, 400);
//populate nums array
for (let n = minNum; n <= maxNum; n++) {
nums.push(n);
}
//select random number from nums
num = random(nums);
}
function draw() {
background(229, 255, 234, 125);
textAlign(CENTER);
//Display instructions
textSize(24);
fill(0, 0, 0);
text("Randomized Flash Cards:", width / 2, 25);
text(minNum + "^2 through " + maxNum + "^2", width / 2, 50);
text("(Click for next question or answer.)", width / 2, 75);
//Display question
textSize(48);
fill(232, 51, 232);
question = num + "^2 " + " = ?";
text(question, width / 2, height / 2);
//If this is not a question card, display answer
if (!isQuestion) {
fill(0, 140, 37);
answer = num ** 2;
text(answer, width / 2, height / 2 + 50);
}
}
function mouseClicked() {
//toggle state between question and answer
isQuestion = !isQuestion;
//if state updated to a question, generate number for next card
if (isQuestion) {
num = random(nums);
}
}