Skip to content

Commit

Permalink
Add multiplication and division
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed Sep 16, 2023
1 parent 094f741 commit 273b325
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 225 deletions.
34 changes: 11 additions & 23 deletions alapmuveleti/addsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function precompute_add_sub_operands() {
for (c = 2 * min_num; c <= max_num; c++) {
for (a = min_num; a <= c - min_num; a++) {
b = c - a;
if (b > max_2nd_operand) {
continue
}
// count things like 20-13 as a "10 crossing" (although it is technically not)
if ((max_num == 20) && (c == 20) && (a > 10)) {
crossing_10s.push({ a, b, c });
Expand All @@ -27,7 +30,7 @@ function precompute_add_sub_operands() {
}
}

// returns the elements of a sequence in a random order
// returns the elements of a sequence in random order
class InRandomOrder {
constructor(sequence) {
this.sequence = randoSequence(sequence);
Expand All @@ -49,19 +52,13 @@ class AddSub {
this.crossing_10_ratio = get_float_input(this.short_name + "_10crossings_ratio") / 100.0;
}

filter(a, b, c) {
return true
}

// returns the next random operation
next() {
do {
if (Math.random() < this.crossing_10_ratio) {
var { a, b, c } = this.operands_crossing_10.next();
} else {
var { a, b, c } = this.operands_not_crossing_10.next();
}
} while (!this.filter(a, b, c));
next(_) {
if (Math.random() < this.crossing_10_ratio) {
var { a, b, c } = this.operands_crossing_10.next();
} else {
var { a, b, c } = this.operands_not_crossing_10.next();
}
return this.render(a, b, c);
}
}
Expand All @@ -72,23 +69,14 @@ class Addition extends AddSub {
return [a, '+', b, '=', c];
}

filter(a, b, c) {
return b <= max_2nd_operand;
}

get short_name() { return "add"; }
}

// Generates random substractions
class Subtraction extends AddSub {
render(a, b, c) {
return [c, '-', a, '=', b];
return [c, '-', b, '=', a];
}

filter(a, b, c) {
return a <= max_2nd_operand;
}


get short_name() { return "sub"; }
}
138 changes: 138 additions & 0 deletions alapmuveleti/columns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Generates the next random operation in a column
class Column {
constructor(x, all_op_generators) {
this.op_generators = all_op_generators.filter(
op => {
return get_bool_input(`${op.short_name}_${x + 1}`);
}
);

if (0 == this.op_generators.length) {
this.op_generators = all_op_generators.slice(); // shallow copy
}

this.random_missing_operand = get_bool_input(`random_missing_operand_${x + 1}`);
}

next() {
let missing_pos = 4;
if (this.random_missing_operand) {
missing_pos = rando([0, 2, 4]).value;
}

let op = rando(this.op_generators).value.next(missing_pos / 2);
op[missing_pos] = "__";
return op;
}
}

function rebuild_all() {
let interval = get_radiobutton_value("allowed_interval");
switch (interval) {
case "100-beginner": {
max_num = 100;
max_2nd_operand = 9;
break;
}
case "100": {
max_num = 100;
max_2nd_operand = max_num;
break;
}
case "20": {
max_num = 20;
max_2nd_operand = max_num;
break;
}
default:
max_num = 10;
max_2nd_operand = max_num;
console.error("Invalid allowed_interval (szamkor) value:", interval);
break;
}
precompute_add_sub_operands();
rebuild_table();
}


function rebuild_table() {
// create a generator for all valid operations
var op_generators = [
new Addition(),
new Subtraction(),
new Multiplication(),
new Division()
];

// collect operation generators for each column
let columns = [];
for (let x = 0; x < column_cnt; x++) {
columns.push(new Column(x, op_generators));
}

// save settings to cookie
// NOTE: the lines above can actually modify the settings
save_settings();

// rewrite the table of exercises
for (let x = 0; x < column_cnt; x++) {
var tbl = document.getElementById(`exercises_${x + 1}`);
tbl.innerHTML = '';
for (let y = 0; y < 25; y++) {
const tr = tbl.insertRow();
parts = columns[x].next();
for (const part of parts) {
const td = tr.insertCell();
td.appendChild(document.createTextNode(part));
}
}
}
}

function build_column_settings() {

var tbl = document.getElementById('per_column_settings');
tbl.innerHTML = `
<tr>
<th colspan="${column_cnt}" style="padding-bottom: 10px;padding-top: 15px;">Művelettípusok kiválasztása oszloponként
(egyszerre több is választható):
</th>
</tr>
`;
const tr = tbl.insertRow();
for (let x = 1; x <= column_cnt; x++) {
const td = tr.insertCell();
td.innerHTML = `
<table class="settings">
<tr>
<td>
<input type="checkbox" id="add_${x}" name="add_${x}" checked="true" onclick="rebuild_table()" />
<label for="add_${x}">Összeadás</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="sub_${x}" name="sub_${x}" checked="true" onclick="rebuild_table()" />
<label for="sub_${x}">Kivonás</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="mul_${x}" name="mul_${x}" checked="true" onclick="rebuild_table()" />
<label for="mul_${x}">Szorzás</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="div_${x}" name="div_${x}" checked="true" onclick="rebuild_table()" />
<label for="div_${x}">Osztás</label>
</td>
</tr>
</table>
<div>
<input type="checkbox" id="random_missing_operand_${x}" name="random_missing_operand_${x}" onclick="rebuild_table()" />
<label for="random_missing_operand_${x}">Kitöltendő hely véletlenszerű pozícióban</label>
</div>
`;
}
}
Loading

0 comments on commit 273b325

Please sign in to comment.