diff --git a/src/app.tsx b/src/app.tsx index 9e0bddc..e4b0323 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -18,7 +18,7 @@ export function App() { } /> - } /> + } /> } /> } /> diff --git a/src/gcf.tsx b/src/gcf.tsx index f80696d..9bb710b 100644 --- a/src/gcf.tsx +++ b/src/gcf.tsx @@ -1,4 +1,4 @@ -import { useState } from 'preact/hooks' +import { useState, useEffect } from 'preact/hooks' import './gcf.css' import { gcf } from './lib' import type { JSX } from 'preact' @@ -37,15 +37,17 @@ function Solution({rows}:SolutionParams) { } +const DEFAULT_MAXNUM = 200 -export function GCF() { + +export function GCF({ maxNum } : { maxNum? : number }) { // choose number between 2 and max inclusive const getRandomInt = (max : number) : number => Math.floor(Math.random() * (max - 1) + 2) - const chooseFactorsRandom = () : [number, number] => [ getRandomInt(200), getRandomInt(200) ] + const chooseFactorsRandom = () : [number, number] => [ getRandomInt(maxNum || DEFAULT_MAXNUM), getRandomInt(maxNum || DEFAULT_MAXNUM) ] const _chooseMultiplesOf = (f : number) : [number, number] => { - const max = Math.floor(200 / f) + const max = Math.floor((maxNum || DEFAULT_MAXNUM) / f) return [ f * getRandomInt(max), f * getRandomInt(max) ] } @@ -54,7 +56,7 @@ export function GCF() { } const chooseMultiples = () : [number, number] => { - const f = getRandomInt(50) + const f = getRandomInt(Math.min(50, Math.floor((maxNum || DEFAULT_MAXNUM) / 2))) return _chooseMultiplesOf(f) } @@ -135,6 +137,16 @@ export function GCF() { const [feedback, setFeedback] = useState(false) const [solution, setSolution] = useState(null) + useEffect(()=>{ + if (maxNum) { + if ((factors[0] > maxNum) || (factors[1] > maxNum)) { + // don't change unless new constraint violated + setFactors(chooseFactors()) + } + } + }, [ maxNum ]) + + return ( Greatest Common Factor Practice diff --git a/src/navbar.tsx b/src/navbar.tsx index 6483fe0..d78024b 100644 --- a/src/navbar.tsx +++ b/src/navbar.tsx @@ -46,7 +46,7 @@ function Navbar({maxNum, onMaxNumChanged} : NavbarSettings) { Max: - + {displayedMax}