Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ <h3>How many worksheets?</h3>
</tr></tbody></table>
</label>
</div>
<div class="col-md-6 pt-3">
<label>
<table><tbody><tr>
<td><div class="p-3"><input id="borrowingChkbx" class="form-control" type="checkbox"/></div></td>
<td><h3 title="Only for subtraction problems. When checked, only generates problems that require borrowing.">Only problems that require borrowing?</h3></td>
</tr></tbody></table>
</label>
</div>
<div class="col-md-12 pt-3">
<div class="w-50 ml-auto mr-auto">
<button class="btn btn-lg btn-primary btn-block p-3" onclick="HwGen.preview()">
Expand Down
46 changes: 38 additions & 8 deletions src/js/hw-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const HwGen = (() => {
const worksheetSelectView = document.getElementById("worksheetSelectView");
const worksheetCountSelect = document.getElementById("worksheetCount");
const answerKeyChkbx = document.getElementById("answerKeyChkbx");
const borrowingChkbx = document.getElementById("borrowingChkbx");
const printConfigView = document.getElementById("printConfigView");
const worksheetView = document.getElementById("worksheetView");
const categorySelectView = document.getElementById("categorySelectView");
Expand All @@ -23,28 +24,54 @@ const HwGen = (() => {
"selectedSet": "",
"selectedCount": 6,
"showAnswerKey": true,
"onlyBorrowing": false,
};
let screenshotInterval = null;
let screenshotNum = 2;
let tipScrollInterval = null;
let tipNum = 0;
const genEquation = (xSize, ySize, mathSymbol) => {
const x = randRangeByDigits(xSize);
const y = randRangeByDigits(ySize);
const z = solution(x, y, mathSymbol);
const genEquation = (xSize, ySize, mathSymbol, onlyBorrowing) => {
let x, y, z;
let attempts = 0;
const maxAttempts = 1000;

do {
x = randRangeByDigits(xSize);
y = randRangeByDigits(ySize);
z = solution(x, y, mathSymbol);
attempts++;

// For subtraction with borrowing constraint
if (mathSymbol === "+" && onlyBorrowing) {
// For subtraction problems, we use mathSymbol "+" but solve for y where z - x = y
// Check if z - x requires borrowing
if (requiresBorrowing(z, x)) {
break;
}
} else {
break;
}
} while (attempts < maxAttempts);

return { x, y, z };
};
const generate = (xSize, ySize, mathSymbol, count, useAllPossible1Digit, myGenEq) => {
const generate = (xSize, ySize, mathSymbol, count, useAllPossible1Digit, myGenEq, onlyBorrowing) => {
let arr = [];
if (useAllPossible1Digit) {
SINGLE_DIGITS.map(x => SINGLE_DIGITS.map(y => arr.push({ x, y, z: solution(x, y, mathSymbol) })));

// Filter for borrowing if needed
if (onlyBorrowing && mathSymbol === "+") {
arr = arr.filter(eq => requiresBorrowing(eq.z, eq.x));
}

arr.sort(() => Math.random() - 0.5); //shuffle
arr.sort(() => Math.random() - 0.5); //shuffle
if (count < arr.length) arr = arr.slice(0, count);
}
else {
for (let i = 0; i < count; i++) {
arr.push(myGenEq ? myGenEq() : genEquation(xSize, ySize, mathSymbol));
arr.push(myGenEq ? myGenEq() : genEquation(xSize, ySize, mathSymbol, onlyBorrowing));
}
}
return arr;
Expand Down Expand Up @@ -119,7 +146,7 @@ const HwGen = (() => {
worksheetList.innerHTML = `<h3>Select ${data["selectedCat"]} Worksheet</h3>` +
hwMap[data["selectedCat"]].map((hwSet, i) => {
const {title, xSize, ySize, mathSymbol, outputFunc, count, name, long, useAllPossible1Digit, myGenEq, myGenEqList} = hwSet;
const eq = myGenEq ? myGenEq() : myGenEqList ? myGenEqList()[0] : genEquation(xSize, ySize, mathSymbol);
const eq = myGenEq ? myGenEq() : myGenEqList ? myGenEqList()[0] : genEquation(xSize, ySize, mathSymbol, false);
const eqStr = outputFunc(eq, -1, 0, long);
return `${i === 0 ? '' : '<hr/>'}<div class="row">
<div class="col-1 text-right pr-0 text-sm">
Expand Down Expand Up @@ -152,10 +179,12 @@ const HwGen = (() => {
}
const { title, count, columns, xSize, ySize, mathSymbol, myGenEqList, outputFunc, answerKey, long, answerSpace, useAllPossible1Digit, myGenEq } = hwSet;
worksheetsDiv.innerHTML = "";
// Only apply borrowing filter for subtraction problems
const onlyBorrowing = data['onlyBorrowing'] && data['selectedCat'] === 'Subtraction';
for (let i = 0; i < data['selectedCount']; i++) {
const worksheet = worksheetOrig.cloneNode(true)
, output = worksheet.querySelector(".output")
, eqList = myGenEqList ? myGenEqList() : generate(xSize, ySize, mathSymbol, count, useAllPossible1Digit, myGenEq)
, eqList = myGenEqList ? myGenEqList() : generate(xSize, ySize, mathSymbol, count, useAllPossible1Digit, myGenEq, onlyBorrowing)
, titleDiv = worksheet.querySelector(".title")
, outputStr = eqList.map((eq, i) => outputFunc(eq, i, columns, long, answerSpace)).join("")
, emoji = randArr(emojis)
Expand Down Expand Up @@ -206,6 +235,7 @@ const HwGen = (() => {
}
data['selectedCount'] = worksheetCountSelect ? parseInt(worksheetCountSelect.value) : 1;
data['showAnswerKey'] = answerKeyChkbx.checked ? true : false;
data['onlyBorrowing'] = borrowingChkbx && borrowingChkbx.checked ? true : false;
};
const init = () => {
const hwSetName = getUrlParam("set");
Expand Down
14 changes: 14 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ const solution = (x, y, mathSymbol) => {
default: return x + y;
}
};
const requiresBorrowing = (minuend, subtrahend) => {
const minuendStr = String(minuend);
const subtrahendStr = String(subtrahend);
const maxLen = Math.max(minuendStr.length, subtrahendStr.length);

for (let i = 0; i < maxLen; i++) {
const minuendDigit = parseInt(minuendStr[minuendStr.length - 1 - i] || 0);
const subtrahendDigit = parseInt(subtrahendStr[subtrahendStr.length - 1 - i] || 0);
if (minuendDigit < subtrahendDigit) {
return true;
}
}
return false;
};
const getUrlParam = param => {
const paramMap = {};
const urlArr = document.location.href.split("?");
Expand Down