An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Evaluating Algebraic Expressions — Elementary Algebra</title>
<link rel="stylesheet" href="../lib/engine.css">
<link rel="expect" href="#bar" blocking="render"><!-- hold the first frame until the player exists, so the page transition can run -->
</head>
<body>
<script src="../lib/engine.js"></script>
<script src="audio/en/timings.js"></script>
<script>
'use strict';
const CHAPTER = { number: 22, title: 'Evaluating algebraic expressions', minutes: 6, next: '../ch23/' };
const VX = COL.nat, VY = COL.whole, RS = COL.irr;
/* ---------- chapter drawing helpers ---------- */
const caption = (p, str, t0, y = 140, fill = COL.dim, size = 30, x = 800) => show(T(p, str, { x, y, size, fill, weight: 600, anchor: 'middle', o: 0 }), t0);
const mline = (p, parts, x, y, t0, fill = COL.chalk, size = 48, anchor = 'middle') => { const e = M(p, [].concat(parts), { x, y, size, fill, anchor, o: 0 }); show(e, t0); return e; };
// A value tag like "x = 4" that the substituted numbers fly out of.
function tag(p, name, value, x, y, color, t0) {
const g = G(p, { x, y, o: 0 });
const e = M(g, [`${name} = ${value}`], { size: 44, fill: color });
mk('rect', { x: -e._w / 2 - 20, y: -46, width: e._w + 40, height: 66, rx: 14, fill: mix(COL.board, color, .15), stroke: color, 'stroke-width': 2.5 }, g);
g.insertBefore(g.lastChild, g.firstChild);
show(g, t0);
return { x, y };
}
// Replace token i of a row by a value that flies in from a tag; returns the new row.
function substitute(p, row, i, parts, from, color, t0) {
const fly = M(p, parts, { x: from.x, y: from.y + 70, size: row.size, fill: color, o: 0 });
show(fly, t0, .2);
tw(fly, { x: row[i]._px, y: row.y }, t0 + .1, .8);
hide(fly, t0 + 1.1, .2);
return collapse(row, i, i, parts, t0 + .3, { color, fill: color });
}
const row = (p, items, y = 330, size = 64, x = 800) => tokens(p, items, { x, y, size, anchor: 'middle', o: 0 });
const showRow = (r, t0) => r.forEach(e => show(e, t0));
/* ---------- beats ---------- */
const BEATS = [
['intro', 'Evaluating expressions', (m, D) => {
const g = G(scene, { o: 0, y: 24 });
T(g, 'Unit 4, Chapter 22', { x: 800, y: 320, size: 28, fill: COL.dim, anchor: 'middle' });
T(g, 'Evaluating algebraic expressions', { x: 800, y: 410, size: 76, weight: 600, anchor: 'middle' });
tw(g, { o: 1, y: 0 }, .2, 1.1, out);
const r = mline(scene, ['3x + 5 when x = ?'], 800, 590, m('q'), COL.dim, 50);
tw(g, { o: 0, y: -24 }, D - .9, .8); hide(r, D - .9, .8);
}],
['sub', 'Substitute, then simplify', (m) => {
const p = panel(0);
const t = tag(p, 'x', 4, 800, 170, VX, .2);
let r = row(p, ['3', { t: 'x', fill: VX }, ' + 5']);
showRow(r, .2);
r = substitute(p, r, 1, ['(4)'], t, VX, m('in', 1));
r = collapse(r, 0, 1, ['12'], m('mul'), { color: COL.real });
r = collapse(r, 0, 1, ['17'], m('add'), { color: COL.int });
mline(p, ['3(4) + 5 = 12 + 5 = 17'], 800, 480, m('ans'), RS, 46);
mline(p, ['x = 10: 3(10) + 5 = 35'], 800, 600, m('other'), COL.dim, 42);
}],
['negative', 'Negative values', (m) => {
const p = panel(0);
const t = tag(p, 'y', '−4', 800, 160, VX, m('q'));
let r = row(p, ['5', { t: 'y', fill: VX }, { t: [E(2)], fill: VX }, ' + 7', { t: 'y', fill: VX }, ' + 3'], 320, 60);
showRow(r, m('q', .5));
r = substitute(p, r, 1, ['(−4)'], t, VX, m('in', .3));
r = substitute(p, r, 4, ['(−4)'], t, VX, m('in', 1.3));
r = collapse(r, 1, 2, ['16'], m('pow', 1), { color: COL.nat });
show(T(p, '(−4)² = 16: the base is −4', { x: 800, y: 420, size: 26, fill: COL.nat, anchor: 'middle', o: 0 }), m('pow', 1));
r = collapse(r, 0, 1, ['80'], m('mul'), { color: COL.real });
r = collapse(r, 1, 2, [' − 28'], m('mul', 2.5), { color: COL.real });
r = collapse(r, 0, 1, ['52'], m('add'), { color: COL.int });
r = collapse(r, 0, 1, ['55'], m('add', 2), { color: COL.int });
mline(p, ['5(−4)', E(2), ' + 7(−4) + 3 = 80 − 28 + 3 = 55'], 800, 560, m('add', 3.5), RS, 42);
}],
['q1', 'Quick check', () => { panel(0); }, { ask: done => quiz(BAND, [{
id: 'c-eval', prompt: ['Evaluate ', $m('4a', E(3), ' + 57'), ' when a = −2.'],
build: blanks([{ parts: ['4(−2)', E(3), ' + 57 = ', { box: '25' }], why: '(−2)³ = −8; 4(−8) = −32; −32 + 57 = 25.', hint: 'Power first: (−2)³ = −8.' }]),
}], done) }],
['two', 'Two variables', (m) => {
const p = panel(0);
const tx = tag(p, 'x', 3, 600, 160, VX, m('q')), ty = tag(p, 'y', '−5', 1000, 160, VY, m('q'));
let r = row(p, ['−6', { t: 'x', fill: VX }, ' + 7', { t: 'y', fill: VY }], 330, 64);
showRow(r, m('q', .5));
r = substitute(p, r, 1, ['(3)'], tx, VX, m('in'));
r = substitute(p, r, 3, ['(−5)'], ty, VY, m('in', 1.2));
r = collapse(r, 0, 1, ['−18'], m('mul'), { color: COL.real });
r = collapse(r, 1, 2, [' − 35'], m('mul', 3), { color: COL.real });
r = collapse(r, 0, 1, ['−53'], m('add'), { color: COL.int });
}],
['fraction', 'A fraction bar', (m) => {
const p = panel(0);
caption(p, 'simplify the top, then the bottom, then divide', m('rule'), 140, COL.task);
tag(p, 'f', 8, 600, 240, VX, m('q')); tag(p, 'g', 3, 1000, 240, VY, m('q'));
mline(p, [F('6f − 4g', '5fg²')], 350, 450, m('q', .5), COL.chalk, 60);
mline(p, ['= ', F('6(8) − 4(3)', '5(8)(3)²')], 700, 450, m('q', 2.5), COL.dim, 52);
mline(p, ['= ', F('36', '')], 1040, 450, m('top'), COL.chalk, 52);
mline(p, ['= ', F('36', '360')], 1040, 450, m('bot'), COL.chalk, 52);
mline(p, ['= ', F(1, 10)], 1300, 450, m('div'), RS, 60);
}],
['q2', 'Quick check', () => { panel(0); }, { ask: done => quiz(BAND, [{
id: 'c-two', prompt: ['Evaluate ', $m('7p', E(2), ' − 6q'), ' when p = −1 and q = 8.'],
build: blanks([{ parts: ['7(−1)', E(2), ' − 6(8) = ', { box: '-41' }], why: '(−1)² = 1; 7 − 48 = −41.', hint: '(−1)² = 1, then subtract 6 × 8.' }]),
}], done) }],
['story', 'Ticket sales', (m) => {
const p = panel(0);
mline(p, ['adult $12, child $7'], 800, 150, m('q'), COL.chalk, 44);
let r = row(p, ['12', { t: 'a', fill: VX }, ' + 7', { t: 'c', fill: VY }], 290, 64);
showRow(r, m('expr'));
show(T(p, 'a = adult tickets, c = child tickets', { x: 800, y: 370, size: 26, fill: COL.dim, anchor: 'middle', o: 0 }), m('expr', 2));
const ta = tag(p, 'a', 40, 560, 470, VX, m('in')), tc = tag(p, 'c', 15, 1040, 470, VY, m('in', .5));
r = substitute(p, r, 1, ['(40)'], ta, VX, m('in', 2));
r = substitute(p, r, 3, ['(15)'], tc, VY, m('in', 3));
r = collapse(r, 0, 1, ['480'], m('calc'), { color: COL.real });
r = collapse(r, 1, 2, [' + 105'], m('calc', 2.5), { color: COL.real });
r = collapse(r, 0, 1, ['585'], m('ans'), { color: COL.int });
mline(p, ['$585 in ticket sales'], 800, 620, m('ans', 1), COL.good, 46);
}],
['q3', 'Quick check', () => {
const p = panel(0);
caption(p, 'Cat food comes in 8-pound bags, dog food in 5-pound bags.', .2, 200, COL.chalk);
caption(p, 'Robin buys c bags of cat food and d bags of dog food.', .5, 260, COL.chalk);
mline(p, ['total pounds = 8c + 5d'], 800, 380, .8, COL.task, 50);
}, { ask: done => quiz(BAND, [{
id: 'c-word', prompt: ['How many pounds when c = 21 and d = 13?'],
build: blanks([{ parts: ['8(21) + 5(13) = ', { box: '233' }, ' pounds'], why: '168 + 65 = 233.', hint: 'Multiply first: 8 × 21 and 5 × 13.' }]),
}], done) }],
['wrap', 'Summary', (m) => {
const p = panel(0);
[['Substitute each value, in parentheses.', 'w1'], ['Simplify with the order of operations.', 'w2'], ['Fraction bar: top, bottom, then divide.', 'w3']].forEach(([str, mark], i) => {
const g = G(p, { x: -16, y: 260 + i * 140, o: 0 });
mk('circle', { cx: 200, cy: -12, r: 26, fill: COL.task }, g);
T(g, String(i + 1), { x: 200, y: -1, size: 30, fill: COL.board, weight: 700, anchor: 'middle' });
T(g, str, { x: 260, size: 34, weight: 600 });
tw(g, { o: 1, x: 0 }, m(mark), .6);
});
}],
['final1', 'Chapter practice', () => { panel(0); }, { ask: done => quiz(SCREEN, [{
id: 'p-one', prompt: ['Evaluate each expression.'], build: blanks(ONE),
}], done, 'Chapter practice 1 of 3') }],
['final2', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-two', prompt: ['Evaluate each expression.'], build: blanks(TWO),
}], done, 'Chapter practice 2 of 3') }],
['final3', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-err', prompt: ['Each answer is wrong. What went wrong?'],
build: grid(ERRQ, [['o', 'Order of operations', COL.real], ['s', 'Negative sign', COL.nat], ['m', 'Forgot to multiply', COL.int]], { multi: false, text: true }),
}], done, 'Chapter practice 3 of 3') }],
['finish', 'Finished', () => {}, { ask: finishCard }],
].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));
/* ---------- chapter practice data ---------- */
const ONE = [
{ parts: ['2x − 9, x = 4: ', { box: '-1' }], why: '8 − 9 = −1.' },
{ parts: ['5y − 3y, y = −7: ', { box: '-14' }], why: '−35 + 21 = −14.' },
{ parts: ['8m', E(2), ' − 12m + 3, m = 1: ', { box: '-1' }], why: '8 − 12 + 3 = −1.' },
{ parts: ['8a', E(2), ' − 6a, a = ', F(1, 2), ': ', { box: '-1' }], why: '8(1/4) − 3 = 2 − 3 = −1.' },
{ parts: ['−x', E(2), ' + 10, x = 3: ', { box: '1' }], why: '−(3²) + 10 = −9 + 10 = 1.' },
];
const TWO = [
{ parts: ['9x − 4y, x = 2, y = 3: ', { box: '6' }], why: '18 − 12 = 6.' },
{ parts: ['xy − y, x = −3, y = 5: ', { box: '-20' }], why: '−15 − 5 = −20.' },
{ parts: [F('8x + y', '4 − xy²'), ', x = −2, y = 2: ', { box: '-7/6' }], why: 'Top: −16 + 2 = −14. Bottom: 4 − (−2)(4) = 12. −14/12 = −7/6.' },
{ parts: [F('a + b', 'a − b'), ', a = 5, b = 3: ', { box: '4' }], why: '8 ÷ 2 = 4.' },
];
const ERRQ = [
{ parts: ['2x², x = 3: (2 · 3)² = 36'], ans: ['o'], why: 'Power first: 2 · 3² = 2 · 9 = 18.' },
{ parts: ['x − y, x = 5, y = −2: 5 − 2 = 3'], ans: ['s'], why: '5 − (−2) = 7: keep the negative in parentheses.' },
{ parts: ['3a, a = −2: 3 − 2 = 1'], ans: ['m'], why: '3a means 3 times a: 3(−2) = −6.' },
{ parts: ['4 + 2x, x = 5: (4 + 2) · 5 = 30'], ans: ['o'], why: 'Multiply first: 4 + 10 = 14.' },
{ parts: ['−x², x = 4: (−4)² = 16'], ans: ['s'], why: 'The base is x: −(4²) = −16.' },
];
boot();
</script>
</body>
</html>