Skip to content

Instantly share code, notes, and snippets.

@zjkuang
Last active May 9, 2022 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zjkuang/d0c9d203b83fdd55e16745181e3e6b5b to your computer and use it in GitHub Desktop.
Save zjkuang/d0c9d203b83fdd55e16745181e3e6b5b to your computer and use it in GitHub Desktop.

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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment