Skip to content

Instantly share code, notes, and snippets.

@1moita
Created September 8, 2021 17:04
Show Gist options
  • Save 1moita/b3da25b76d2809c837313ad272cf96f4 to your computer and use it in GitHub Desktop.
Save 1moita/b3da25b76d2809c837313ad272cf96f4 to your computer and use it in GitHub Desktop.
Create random calculations for quizzes.
function generate() {
const random = (min, max) => Math.floor(Math.random() * (max - min) + min);
const numbers = [random(1, 100), random(1, 100)];
const pointer = ['+', '-', '*', '/'][Math.floor(Math.random() * ['+', '-', '*', '/'].length)];
let result;
try {
result = eval(`${numbers[0]} ${pointer} ${numbers[1]}`);
} catch {
throw console.error('Can\'t eval calc');
}
return { numbers, pointer, result };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment