Skip to content

Instantly share code, notes, and snippets.

@lukKowalski
Last active March 16, 2020 21:21
Show Gist options
  • Save lukKowalski/391df935f1be24b1668d642ab046a625 to your computer and use it in GitHub Desktop.
Save lukKowalski/391df935f1be24b1668d642ab046a625 to your computer and use it in GitHub Desktop.
Generuje tabliczkę mnożenia/dzielenia do 50, po 18 w słupku. Wymagany node
#! /usr/bin/env node
//Generuje tabliczkę mnozenia/dzielenia w dwoch kolumnach, do 50, po 18 w slupku
//Wymagany: node
//uruchomienie: ./generujTabliczke_xD.js >> tabliczka (wrzuci tabliczke do pliku "tabliczka")
// 2 * 9 = ___ 16 : 2 = ___
// 9 * 1 = ___ 35 : 7 = ___
// 5 * 6 = ___ 30 : 3 = ___
// 7 * 4 = ___ 5 : 5 = ___
// 9 * 6 = ___ 45 : 5 = ___
// 2 * 2 = ___ 18 : 9 = ___
// 2 * 3 = ___ 24 : 8 = ___
// 2 * 6 = ___ 40 : 8 = ___
// 1 * 6 = ___ 15 : 3 = ___
// 1 * 3 = ___ 10 : 10 = ___
// 1 * 2 = ___ 50 : 5 = ___
// 3 * 7 = ___ 24 : 6 = ___
// 9 * 4 = ___ 8 : 8 = ___
// 1 * 1 = ___ 42 : 7 = ___
// 4 * 3 = ___ 27 : 3 = ___
// 8 * 4 = ___ 7 : 1 = ___
// 7 * 2 = ___ 40 : 10 = ___
// 8 * 6 = ___ 18 : 6 = ___
const randomInt = max => Math.round(Math.random() * max);
const randomPair = max => {
const first = randomInt(max);
const second = randomInt(
Math.min(max, (max + Math.floor(max/2)) - first)
)
return [first, second];
}
const ifLastThreeContainPair = (pair, list) => {
let i = list.length - 1;
while (i > 0) {
const e = list[i];
if (
e[0] == pair[0] && e[1] == pair[1] ||
e[0] == pair[1] && e[1] == pair[0]
) {
return true;
}
i--;
}
return false;
}
const generateList = (max, amount) => {
const arr = [];
while(arr.length < amount) {
const pair = randomPair(max);
if (pair[0] && pair[1] && !ifLastThreeContainPair(pair, arr)) {
arr.push(pair);
}
}
return arr;
}
const createMultiply = pair => `${pair[0]} * ${pair[1]} = ___`;
const createDivision = pair => {
const result = pair[0] * pair[1];
if (Math.random() >= 0.5) {
return `${result} : ${pair[0]} = ___`;
} else {
return `${result} : ${pair[1]} = ___`;
}
}
const space = " ";
const br = "\n";
let str = "";
const list = generateList(10, 36);
list.forEach((el, idx) => {
if (idx % 2 > 0) {
str += createDivision(el);
str += br;
} else {
str += createMultiply(el);
str += space;
}
});
console.log(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment