Integer Operations Worksheet Generator
Generate a fresh 20-problem practice set on integer operations: addition, subtraction (including double negatives),
multiplication, division, plus temperature and money word problems.
Skill: integer-operations
Each click of Generate builds a brand-new set of 20 problems. Toggle the answer key any time; printing includes the key only when it is visible.
Integer Operations Practice Worksheet
Answer Key
For the teacher or for self-checking after you finish.
/* ============================================================ Integer Operations Worksheet — generator core (pure, no DOM). UMD: required by node tests as a module, inlined into worksheet.html as window.IntegerWorksheet. ============================================================ */ (function (root, factory) { 'use strict'; var api = factory(); if (typeof module === 'undefined') { root.IntegerWorksheet = api; } else { module.exports = api; } })(typeof self !== 'undefined' ? self : this, function () { 'use strict';
var MINUS = '\u2212'; function fmtI(n) { return String(n).replace(/-/g, MINUS); } function paren(n) { return n < 0 ? '(' + fmtI(n) + ')' : String(n); } /* ---------------- seeded RNG ---------------- */ function mulberry32(a) { return function () { a |= 0; a = (a + 0x6D2B79F5) | 0; var t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } var _rand = Math.random; function setSeed(s) { _rand = mulberry32(s >>> 0); } function ri(lo, hi) { return lo + Math.floor(_rand() * (hi - lo + 1)); } function pick(arr) { return arr[Math.floor(_rand() * arr.length)]; } function nz(lo, hi) { var v = 0; while (v === 0) v = ri(lo, hi); return v; } function chance(p) { return _rand() < p; } function shuffle(a) { for (var i = a.length - 1; i > 0; i--) { var j = Math.floor(_rand() * (i + 1)); var t = a[i]; a[i] = a[j]; a[j] = t; } return a; }
/* Each generator returns { stemHTML, answerHTML, answer:{k:'i', v} } */ function P(stemHTML, v) { return { stemHTML: stemHTML, answerHTML: '' + fmtI(v) + '', answer: { k: 'i', v: v } }; }
function genAdd(diff) { var a, b; if (diff === 'easy') { a = ri(1, 9); b = ri(1, 9); if (chance(0.5)) a = -a; if (chance(0.3)) b = -b; } else if (diff === 'medium') { a = ri(-15, 15); b = nz(-15, 15); } else { a = ri(-25, 25); b = nz(-25, 25); if (chance(0.3)) b = -Math.abs(b); } if (a === 0) { if (b === 0) { a = 4; } } return P('Compute ' + fmtI(a) + ' + ' + paren(b) + '.', a + b); }
function genSub(diff) { var a, b, v; if (diff === 'easy') { a = ri(2, 12); b = ri(1, 9); if (chance(0.4)) { var t = a; a = Math.min(a, b); b = Math.max(t, b) + 1; } v = a - b; return P('Compute ' + a + ' \u2212 ' + b + '.', v); } a = ri(-15, 15); b = nz(-15, 15); if (diff === 'hard') { if (chance(0.5)) b = -Math.abs(b); } if (diff === 'hard') { if (chance(0.25)) { a = -Math.abs(a); } } v = a - b; return P('Compute ' + fmtI(a) + ' \u2212 ' + paren(b) + '.', v); }
function genMul(diff) { var a, b; if (diff === 'easy') { a = ri(2, 9); b = ri(2, 9); if (chance(0.35)) a = -a; } else if (diff === 'medium') { a = nz(-9, 9); b = nz(-9, 9); } else { a = nz(-12, 12); b = nz(-12, 12); } return P('Compute (' + fmtI(a) + ')(' + fmtI(b) + ').', a * b); }
function genDiv(diff) { var q = nz(2, diff === 'hard' ? 12 : 9), d = nz(2, diff === 'hard' ? 12 : 9); var dd = Math.abs(q) * d; var stop = chance(0.5) ? -dd : dd; var sbot = chance(0.5) ? -d : d; var v = (stop < 0) === (sbot < 0) ? Math.abs(q) : -Math.abs(q); return P('Compute ' + fmtI(stop) + ' \u00F7 ' + paren(sbot) + '.', v); } function genWord(diff) { if (chance(0.5)) { var t = ri(2, diff === 'hard' ? 25 : 15), r = t + ri(1, 12); return P('The temperature was ' + fmtI(-t) + '\u00B0F and rose ' + r + '\u00B0F. What is the new temperature?', r - t); } var b = ri(2, 9) * 5, s = b + ri(1, 8) * 5; return P('Your account balance is $' + b + '. You spend $' + s + '. What is your new balance?', b - s); } function genMulti(diff) { var a = nz(-15, 15), b = nz(-15, 15), c = nz(-15, 15); var mid = a + b; if (mid === 0) { b += 2; mid = a + b; } var v = mid - c; if (v === 0) { c += 3; v = mid - c; } return P('Compute ' + fmtI(a) + ' + ' + paren(b) + ' \u2212 ' + paren(c) + '.', v); } var PLANS = { easy: ['add', 'add', 'add', 'add', 'add', 'add', 'sub', 'sub', 'sub', 'sub', 'sub', 'sub', 'mul', 'mul', 'mul', 'mul', 'div', 'div', 'word', 'word'], medium: ['add', 'add', 'add', 'add', 'add', 'sub', 'sub', 'sub', 'sub', 'sub', 'mul', 'mul', 'mul', 'mul', 'div', 'div', 'div', 'word', 'word', 'multi'], hard: ['add', 'add', 'add', 'add', 'sub', 'sub', 'sub', 'sub', 'sub', 'mul', 'mul', 'mul', 'mul', 'div', 'div', 'div', 'word', 'word', 'multi', 'multi'] }; var GEN = { add: genAdd, sub: genSub, mul: genMul, div: genDiv, word: genWord, multi: genMulti }; function generateSet(seed, difficulty) { if (['easy', 'medium', 'hard'].indexOf(difficulty) === -1) throw new Error('generateSet: bad difficulty ' + difficulty); setSeed(seed >>> 0); var plan = shuffle(PLANS[difficulty].slice()); var problems = plan.map(function (kind) { return GEN[kind](difficulty); }); if (problems.length !== 20) throw new Error('generateSet: expected 20 problems'); return problems; }
return { generateSet: generateSet, fmtI: fmtI, PLANS: PLANS }; });
/* Shared Fibo worksheet page glue (browser only). Config (inline script before this file): window.FIBO_WORKSHEET = { core: 'IntegerWorksheet' } Core API (pure, node-tested): generateSet(seed, difficulty) -> array of { stemHTML, answerHTML }. */ (function () { 'use strict'; var CFG = window.FIBO_WORKSHEET || {}; var CORE = window[CFG.core || '']; if (!CORE || !document.getElementById('diffSel')) { return; }
var current = { problems: [], difficulty: 'medium', keyVisible: false, seed: 0, genCount: 0 }; var el = {};
function renderProblems() { el.problems.innerHTML = current.problems.map(function (p, i) { return '
'; }).join(''); el.probCount.textContent = current.problems.length + ' problems \u00B7 ' + current.difficulty.charAt(0).toUpperCase() + current.difficulty.slice(1); }
function renderKey() { el.keyGrid.innerHTML = current.problems.map(function (p, i) { return '
'; }).join(''); }
function newSet(seed) { current.seed = (seed === undefined) ? ((Date.now() + (current.genCount * 7919)) >>> 0) : (seed >>> 0); current.genCount++; current.problems = CORE.generateSet(current.seed, current.difficulty); renderProblems(); if (current.keyVisible) { renderKey(); } }
function setKeyVisible(v) { current.keyVisible = v; el.keySection.hidden = !v; el.btnKey.setAttribute('aria-pressed', v ? 'true' : 'false'); el.btnKey.textContent = v ? 'Hide Answer Key' : 'Show Answer Key'; if (v) { renderKey(); } }
function init() { el.diffSel = document.getElementById('diffSel'); el.btnGen = document.getElementById('btnGen'); el.btnKey = document.getElementById('btnKey'); el.btnPrint = document.getElementById('btnPrint'); el.problems = document.getElementById('problems'); el.probCount = document.getElementById('probCount'); el.keySection = document.getElementById('keySection'); el.keyGrid = document.getElementById('keyGrid');
current.difficulty = el.diffSel.value;
el.diffSel.addEventListener('change', function () { current.difficulty = el.diffSel.value; newSet(); }); el.btnGen.addEventListener('click', function () { newSet(); }); el.btnKey.addEventListener('click', function () { setKeyVisible(!current.keyVisible); }); el.btnPrint.addEventListener('click', function () { window.print(); });
newSet(); }
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); }
window.__WS = { generateSet: CORE.generateSet, getState: function () { return current; }, newSet: newSet, setKeyVisible: setKeyVisible }; })();