Skip to content

Commit

Permalink
More worksheets & optional answer key
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewis Nakao committed Jul 20, 2020
1 parent 0fa6459 commit 91ffd7c
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 203 deletions.
51 changes: 20 additions & 31 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<!doctype html><html><head><meta charset="utf-8">
<title>Random Math Homework Generator</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Automatically generate new random print-friendly homework worksheets for math practice.">
<meta name="author" content="Lewis Nakao">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>Random Math Homework Generator</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Automatically generate new random print-friendly homework worksheets for math practice.">
<meta name="author" content="Lewis Nakao">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body class="bg-light">
<body class="bg-light main">
<div class="container">
<h1>Random Math Homework Generator</h1>
<p><strong>HW Gen</strong> will automatically generate new random print-friendly homework worksheets for math practice. Just click on a link to view the homework. Hit refresh to get a new set.</p>
Expand All @@ -15,30 +16,18 @@ <h4>Features</h4>
<li>Print-friendly</li>
<li>Answers can be filled and then printed too.</li>
</ul>
<table class="table"><tbody id="output">
</tbody></table>
<label class="d-block">
<div class="d-inline-block">
<input id="answerKeyCbx" type="checkbox" class="form-control" checked
onclick="toggleAnswerKey(this)"/>
</div>
<div class="d-inline-block">Include Answer Key</div>
</label>
<table class="table"><tbody id="output"></tbody></table>
<div class="text-center text-muted">
Author: <a href="http://lewdev.github.io">Lewis Nakao</a>
</div>
<script type="text/javascript">
window.onload = () => {
const output = document.getElementById("output")
, arr = []
;
output.innerHTML = data.map((a, i) => `<tr>
<td><span class="mr-2">${i + 1}.</span> <a href="${a.file}" target="_blank">${a.title}</a></td>
</tr>`).join("");
};
const data = [
{ file: "multiplication.html", title: "Multiplication Single-digit " },
{ file: "multiplication-2-1.html", title: "Multiplication 2 to 1-digit" },
{ file: "multiplication-2-2.html", title: "Multiplication 2 to 2-digit" },
];

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-2833478-13', 'auto');
ga('send', 'pageview');
</script></body></html>
</div>
<script src="js/hw-gen.js"></script>
<script src="js/hw-sets.js"></script>
<script src="js/index.js"></script>
<script src="js/google-analytics.js" type="text/javascipt"></script></body></html>
6 changes: 6 additions & 0 deletions src/js/google-analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-2833478-13', 'auto');
ga('send', 'pageview');
74 changes: 74 additions & 0 deletions src/js/hw-gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

const rand = max => Math.floor(Math.random() * max);
const randRange = (min, max) => rand(max - min) + min;
const randArr = arr => arr[rand(arr.length)];
const singleDigits = [2,3,4,5,6,7,8,9];
const randNoOnes = () => randArr(singleDigits);
const randRangeByDigits = (digits, noOnes = false) => {
if (noOnes && digits === 1) return randNoOnes();
const start = Math.pow(10, digits - 1);
const end = Math.pow(10, digits) - 1;
console.log({start, end})
return randRange(start, end);
};
const solution = (x, y, mathSymbol) => {
switch (mathSymbol) {
case "*": return x * y;
case "/": return x / y;
case "-": return x - y;
default: return x + y;
}
};
const getUrlParam = param => {
const paramMap = {};
const urlArr = document.location.href.split("?");
const queryParam = urlArr.length > 1 ? urlArr[1] : false;
if (queryParam) {
var paramArr = queryParam.split("&");
paramArr.map(paramSet => {
if (paramSet.indexOf(param + "=") === 0) {
paramMap[param] = paramSet.split("=")[1].replace(/%20/g, " ");
}
});
}
return paramMap[param] || false;
};
const genEquation = (xSize, ySize, mathSymbol, noOnes = false) => {
const x = randRangeByDigits(xSize, noOnes);
const y = randRangeByDigits(ySize, noOnes);
const z = solution(x, y, mathSymbol);
return { x, y, z };
};
const generate = (xSize, ySize, mathSymbol, count) => {
const arr = [];
if (xSize === 1 && ySize === 1) {
singleDigits.map(x => singleDigits.map(y => arr.push({ x, y, z: solution(x, y, mathSymbol) })));
arr.sort(() => Math.random() - 0.5); //shuffle
arr.sort(() => Math.random() - 0.5); //shuffle
}
else {
for (let i = 0; i < count; i++) {
arr.push(genEquation(xSize, ySize, mathSymbol));
}
}
return arr;
};
window.onload = () => {
const setName = getUrlParam("set") || "addition"
//, copies = getUrlParam("copies") || 1
, showAnswerKey = getUrlParam("showAnswerKey") || false
, output = document.getElementById("output")
, answerKey = document.getElementById("answerKey")
, titleDiv = document.getElementById("title")
, hwSet = hwSets[setName]
, { title, count, columns, xSize, ySize, mathSymbol, outputFunc, noOnes } = hwSet
, arr = generate(xSize, ySize, mathSymbol, count, noOnes)
, outputStr = arr.map((eq, i) => outputFunc(eq, i, columns)).join("")
, answersStr = showAnswerKey ? '<div class="answer-key-table">' + arr.map(hwSet.answerKey).map((a, i) =>
`<span class="answer-key">${i + 1}.) ${a}</span>`).join("") + '</div>' : ''
;
document.title = title;
titleDiv.innerHTML = title;
output.innerHTML = '<tr>' + outputStr + '</tr>';
answerKey.innerHTML = answersStr;
};
112 changes: 112 additions & 0 deletions src/js/hw-sets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const hwSets = {
"addition": {
title: "Addition 1-digit Equations",
count: 20,
columns: 3,
xSize: 1,
ySize: 1,
mathSymbol: "+",
outputFunc: (eq, i, columns) => `
<td class="inline-equation text-nowrap">
<span class="number">${i + 1}.)</span>${eq.x} + ${eq.y} =
</td>
<td class="answer"><input type="text" class="answer-input-d"/></td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.z,
},
"addition-find-addends": {
title: "Addition Find Addends 1-digit Equations",
count: 20,
columns: 3,
xSize: 1, ySize: 1, mathSymbol: "+",
outputFunc: (eq, i, columns) => `
<td class="inline-equation text-nowrap">
<span class="text-muted mr-2 number">${i + 1}.)</span>
${eq.x} + <input type="text" class="answer-input-d"/> =
</td>
<td class="answer">${eq.z}</td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.y,
},
"addition-2-1": {
title: "Addition 2-1-digit Equations",
count: 40, columns: 4,
xSize: 2, ySize: 1, noOnes: true, mathSymbol: "+",
outputFunc: (eq, i, columns) => `
<td><span class="number">${i + 1}.)</span></td>
<td class="pb-5">
<div class="equation">${eq.x}</div>
<div class="equation">+ ${eq.y}</div>
<div class=" equation"><input type="text" class="answer-input"/></div>
</td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.z,
},
"addition-2-2": {
title: "Addition 2-2-digit Equations",
count: 40, columns: 4,
xSize: 2, ySize: 2, noOnes: true, mathSymbol: "+",
outputFunc: (eq, i, columns) => `
<td><span class="number">${i + 1}.)</span></td>
<td class="pb-5">
<div class="equation">${eq.x}</div>
<div class="equation">+ ${eq.y}</div>
<div class=" equation"><input type="text" class="answer-input"/></div>
</td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.z,
},
"multiplication": {
title: "Multiplication 1-digit Equations",
count: 64,
columns: 3,
xSize: 1,
ySize: 1,
mathSymbol: "*",
outputFunc: (eq, i, columns) => `
<td class="inline-equation text-nowrap">
<span class="number">${i + 1}.)</span> ${eq.x} &times; ${eq.y} =
</td>
<td class="answer">
<input type="text" class="answer-input-d"/>
</td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.z,
},
"multiplication-find-multiple": {
title: "Multiplication Find Multiple 1-1-digit Equations",
count: 20,
columns: 3,
xSize: 1,
ySize: 1,
mathSymbol: "*",
outputFunc: (eq, i, columns) => `
<td class="inline-equation text-nowrap">
<span class="number">${i + 1}.)</span> ${eq.x} &times;
<input type="text" class="answer-input-d"/> =
</td>
<td class="answer">
${eq.z}
</td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.y,
},
"multiplication-2-1": {
title: "Muliplication 2 to 1-digit Equations",
count: 40,
columns: 4,
xSize: 2,
ySize: 1,
mathSymbol: "*",
noOnes: true,
outputFunc: (eq, i, columns) => `
<td><span class="number">${i + 1}.)</span></td>
<td class="pb-5">
<div class="equation">${eq.x}</div>
<div class="equation">&times; ${eq.y}</div>
<div class=" equation"><input type="text" class="answer-input"/></div>
</td>
${((i + 1) % columns) === 0 ? '</tr><tr>' : ''}`,
answerKey: eq => eq.y,
},
};
28 changes: 28 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let answerKeyCbx = document.getElementById("answerKeyCbx");
let answerKey = answerKeyCbx.checked;
const toggleAnswerKey = elem => {
answerKey = elem.checked;
render();
};
window.onload = () => render();
const render = () => {
const output = document.getElementById("output");
output.innerHTML = Object.keys(hwSets).map((a, i) => {
const {title, xSize, ySize, mathSymbol, outputFunc, count} = hwSets[a];
const eq = genEquation(xSize, ySize, mathSymbol);
const eqStr = outputFunc(eq, -1, 0);
return `<tr>
<td class="text-right pr-0 text-sm">
<span class="mr-2 number">${i + 1}.</span>
</td>
<td>
<a href="worksheet.html?set=${a}${answerKey ? '&showAnswerKey=1' : ''}" target="_blank">${title}</a>
${answerKey ? '<div class="text-muted">w/answer key</div>' : ''}
${xSize === 1 && ySize === 1 ? 64 : count} Problems
</td>
<td>e.g.</td>
<td style="width:10rem;">
<table class="w-50"><tbody><tr class="example">${eqStr}</tr></tbody></table>
</td>
</tr>`}).join("");
};
59 changes: 0 additions & 59 deletions src/multiplication-2-1.html

This file was deleted.

Loading

0 comments on commit 91ffd7c

Please sign in to comment.