Created
September 8, 2021 17:04
-
-
Save 1moita/b3da25b76d2809c837313ad272cf96f4 to your computer and use it in GitHub Desktop.
Create random calculations for quizzes.
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
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