Sophie's Maths
In Terminal,
npx <url-of-this-gist>
IMPORTANT: If you have made any changes in the script, to make the changes visible, make sure you update the version number in package.json
| #!/usr/bin/env node | |
| function random(min, max, exclude) { | |
| let a; | |
| do { | |
| a = Math.floor(Math.random() * (max - min + 1) + min); | |
| } while (a === exclude); | |
| return a; | |
| } | |
| function duplicated(a, e, exchangeable) { | |
| const same = a.find(element => { | |
| if (element[0] === e[0] && element[1] === e[1]) { | |
| return true; | |
| } | |
| if (exchangeable && element[0] === e[1] && element[1] === e[0]) { | |
| return true; | |
| } | |
| return false; | |
| }); | |
| return !!same; | |
| } | |
| function addition() { | |
| const challenge = []; | |
| do { | |
| const e = [random(1, 20), 9]; | |
| if (!duplicated(challenge, e, true)) { | |
| challenge.push(e); | |
| } | |
| } while (challenge.length < 12); | |
| challenge.map(e => { | |
| console.log(`${e[0]} + ${e[1]} =`); | |
| }); | |
| } | |
| function subtraction() { | |
| const challenge = []; | |
| do { | |
| let a; | |
| let b; | |
| do { | |
| a = random(10, 20); | |
| b = 9; | |
| } while (a <= b); | |
| const e = [a, b]; | |
| if (!duplicated(challenge, e, false)) { | |
| challenge.push(e); | |
| } | |
| } while (challenge.length < 11); | |
| challenge.map(e => { | |
| console.log(`${e[0]} - ${e[1]} =`); | |
| }); | |
| } | |
| console.log(''); | |
| addition(); | |
| console.log(''); | |
| subtraction(); |
| { | |
| "name": "maths-for-fattie-eggie", | |
| "version": "1.0.2", | |
| "bin": "./index.js" | |
| } |