QuadraticEquationQuestion.jsx (2952B)
1 import { formatCoefficient, formatFraction } from '../../scripts/formatUtils.js'; 2 import { getHCF } from '../../scripts/mathsUtils.js'; 3 import { getRandomSelection, getRandomChoice, getRandomInt } from '../../scripts/randomUtils.js'; 4 import Question from '../Question.jsx'; 5 6 function formatQuadraticExpression(a, b, c) { 7 const fa = formatCoefficient(a, { showPlus: false, showOne: false, showZero: false, suffix: 'x^2' }); 8 const fb = formatCoefficient(b, { showPlus: true, showOne: false, showZero: false, suffix: 'x' }); 9 const fc = formatCoefficient(c, { showPlus: true, showOne: true, showZero: false }); 10 return `${fa} ${fb} ${fc}`; 11 } 12 13 function formatRoots(l, m, n, o) { 14 if (m / l == o / n) return `x = ${formatFraction(-m, l, { simplify: true })}`; 15 if (-m / l == o / n) return `x = \\pm ${formatFraction(Math.abs(m), l, { simplify: true })}`; 16 17 return `x = ${formatFraction(-m, l, { simplify: true })} \\text{ or } x = ${formatFraction(-o, n, { simplify: true })}`; 18 } 19 20 function formatFactor(x_coeff, const_coeff) { 21 if (const_coeff == 0) return "x"; 22 const fx_coeff = formatCoefficient(x_coeff, { showPlus: false, showOne: false, showZero: false, suffix: 'x' }) 23 const fconst_coeff = formatCoefficient(const_coeff, { showPlus: true, showOne: true, showZero: false }) 24 return `(${fx_coeff}${fconst_coeff})`; 25 } 26 27 function formatFactors(l, m, n, o) { 28 const hcf1 = Math.abs(getHCF(l, m)) 29 const hcf2 = Math.abs(getHCF(n, o)) 30 const frontCoefficient = hcf1 * hcf2 31 l /= hcf1 32 m /= hcf1 33 n /= hcf2 34 o /= hcf2 35 36 const fFrontCoefficient = formatCoefficient(frontCoefficient, {showPlus: false, showOne: false, showZero: true}) 37 if (l == n && m == o) return `${fFrontCoefficient}${formatFactor(l, m)}^2` 38 return `${fFrontCoefficient}${formatFactor(l, m)}${formatFactor(n, o)}` 39 } 40 41 export default function QuadraticEquationQuestion({ seed, showAnswer, aPlurality, framing, cSigns }) { 42 // (lx + m)(nx + o) 43 44 const factorise = getRandomSelection(framing, { "Solve" : [false], "Factorise": [true] }, seed++); 45 const cSign = getRandomSelection(cSigns, { "Positive": [1], "Negative" : [-1] }, seed++); 46 let [l, n] = getRandomSelection(aPlurality, { 47 "a=1": [[1, 1]], 48 "a>1": [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 2], [2, 3]], 49 }, seed++); 50 51 const mSign = getRandomChoice([-1, 1], seed++) 52 53 let m = getRandomInt(1, 9, seed++) * mSign 54 let o = getRandomInt(1, 9, seed++) * mSign * cSign 55 56 // order by root value 57 if (-m/l > -o/n) { 58 [l, m, n, o] = [n, o, l, m] 59 } 60 61 // lnx^2 + (lo + nm)x + mo 62 const a = l * n 63 const b = (l * o) + (n * m) 64 const c = m * o 65 66 const quadratic = formatQuadraticExpression(a, b, c); 67 const question = factorise ? `\\text{Factorise } ${quadratic}` : `\\text{Solve } ${quadratic} = 0`; 68 69 const answer = factorise ? formatFactors(l, m, n, o) : formatRoots(l, m, n, o); 70 71 return <Question question={question} answer={answer} showAnswer={showAnswer} />; 72 73 }