website

source code for the ttmaths.uk website
Log | Files | Refs | README

commit 62f9e724be31d16ed94413eafdfc516d68e96359
parent 2334d6782239763eee94cc35f3b7c6c77cb6ca2d
Author: fen123456 <fenwthompson@gmail.com>
Date:   Fri, 14 Aug 2026 15:12:42 +0100

Move fraction formatting to utils

Diffstat:
Msrc/components/questions/AddingFractionsQuestion.jsx | 12+-----------
Msrc/components/questions/QuadraticEquationQuestion.jsx | 13+------------
Msrc/scripts/utils.js | 15++++++++++++++-
3 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/src/components/questions/AddingFractionsQuestion.jsx b/src/components/questions/AddingFractionsQuestion.jsx @@ -1,15 +1,5 @@ import Question from '../Question.jsx'; -import { getRandomInt, getHCF } from '../../scripts/utils.js'; - -function formatFraction(num, denom) { - if (denom == 1) { return `${num}`; } - return num + '/' + denom; -} - -function simplifyFraction(num, denom) { - const hcf = getHCF(num, denom); - return [num / hcf, denom / hcf]; -} +import { getRandomInt, getHCF, formatFraction, simplifyFraction } from '../../scripts/utils.js'; function getRandomFraction(seed) { const num0 = getRandomInt(1, 5, seed++); diff --git a/src/components/questions/QuadraticEquationQuestion.jsx b/src/components/questions/QuadraticEquationQuestion.jsx @@ -1,5 +1,5 @@ import Question from '../Question.jsx'; -import { getRandomInt, getRandomBoolean, getRandomChoice, formatCoefficient, getHCF } from '../../scripts/utils.js'; +import { getRandomInt, getRandomBoolean, getRandomChoice, formatCoefficient, getHCF, formatFraction } from '../../scripts/utils.js'; function formatQuadraticExpression(a, b, c) { const fa = formatCoefficient(a, { showPlus: false, showOne: false, showZero: false, suffix: 'x^2' }); @@ -8,17 +8,6 @@ function formatQuadraticExpression(a, b, c) { return `${fa} ${fb} ${fc}`; } -function formatFraction(num, denom) { - const hcf = getHCF(Math.abs(num), Math.abs(denom)) - const prefix = num / denom > 0 ? "" : "-" - - num = Math.abs(num / hcf) - denom = Math.abs(denom / hcf) - - if (denom == 1) { return `${prefix}${num}`; } - return `${prefix}frac(${num}, ${denom})` -} - function formatRoots(l, m, n, o) { if (m / l == o / n) { return `x = ${formatFraction(-m, l)}`; } if (-m / l == o / n) { return `x = plus.minus ${formatFraction(Math.abs(-m), l)}`; } diff --git a/src/scripts/utils.js b/src/scripts/utils.js @@ -20,7 +20,7 @@ export function getHCF(a, b) { return b == 0 ? a : getHCF(b, a % b); } -export function formatCoefficient(x, parameters = {showPlus: false, showOne: true, showZero: true, suffix: ''}) { +export function formatCoefficient(x, parameters = { showPlus: false, showOne: true, showZero: true, suffix: '' }) { if (!parameters.showZero && x == 0) { return ''; } const plus = parameters.showPlus ? '+' : ''; const sign = x < 0 ? '-' : plus; @@ -30,6 +30,19 @@ export function formatCoefficient(x, parameters = {showPlus: false, showOne: tru return sign + x + parameters.suffix; } +export function simplifyFraction(num, denom) { + const hcf = getHCF(num, denom); + return [num / hcf, denom / hcf]; +} + +export function formatFraction(num, denom, parameters = { showPlus: false, simplify: false }) { + const plus = parameters.showPlus ? '+' : ''; + const sign = num / denom < 0 ? '-' : plus; + [num, denom] = parameters.simplify ? [num, denom] : simplifyFraction(num, denom) + if (denom == 1) { return `${sign}${Math.abs(num)}`; } + return `${sign}frac(${num}, ${denom})` +} + export function getSymmetricBounds(midX, viewBox) { const [ minX, minY, width, height ] = viewBox; const halfWidth = Math.max(midX - minX, minX + width - midX);