Make max slider bar work for GCF

This commit is contained in:
2026-04-23 14:25:16 -04:00
parent 8a7b952deb
commit 45a11a379a
3 changed files with 19 additions and 7 deletions

View File

@@ -18,7 +18,7 @@ export function App() {
<Navbar maxNum={maxNum} onMaxNumChanged={onMaxNumSet} /> <Navbar maxNum={maxNum} onMaxNumChanged={onMaxNumSet} />
<Routes> <Routes>
<Route path="/" element={<Navigate to="/gcf" />} /> <Route path="/" element={<Navigate to="/gcf" />} />
<Route path="/gcf" element={<GCF />} /> <Route path="/gcf" element={<GCF maxNum={maxNum} />} />
<Route path="/times" element={<TimesPractice maxNum={12} />} /> <Route path="/times" element={<TimesPractice maxNum={12} />} />
<Route path="/lcm" element={<LCM />} /> <Route path="/lcm" element={<LCM />} />
</Routes> </Routes>

View File

@@ -1,4 +1,4 @@
import { useState } from 'preact/hooks' import { useState, useEffect } from 'preact/hooks'
import './gcf.css' import './gcf.css'
import { gcf } from './lib' import { gcf } from './lib'
import type { JSX } from 'preact' import type { JSX } from 'preact'
@@ -37,15 +37,17 @@ function Solution({rows}:SolutionParams) {
</div> </div>
} }
const DEFAULT_MAXNUM = 200
export function GCF() {
export function GCF({ maxNum } : { maxNum? : number }) {
// choose number between 2 and max inclusive // choose number between 2 and max inclusive
const getRandomInt = (max : number) : number => Math.floor(Math.random() * (max - 1) + 2) 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 _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) ] return [ f * getRandomInt(max), f * getRandomInt(max) ]
} }
@@ -54,7 +56,7 @@ export function GCF() {
} }
const chooseMultiples = () : [number, number] => { const chooseMultiples = () : [number, number] => {
const f = getRandomInt(50) const f = getRandomInt(Math.min(50, Math.floor((maxNum || DEFAULT_MAXNUM) / 2)))
return _chooseMultiplesOf(f) return _chooseMultiplesOf(f)
} }
@@ -135,6 +137,16 @@ export function GCF() {
const [feedback, setFeedback] = useState(false) const [feedback, setFeedback] = useState(false)
const [solution, setSolution] = useState<JSX.Element|null>(null) const [solution, setSolution] = useState<JSX.Element|null>(null)
useEffect(()=>{
if (maxNum) {
if ((factors[0] > maxNum) || (factors[1] > maxNum)) {
// don't change unless new constraint violated
setFactors(chooseFactors())
}
}
}, [ maxNum ])
return ( return (
<div className="px-4 py-5 my-5 text-center"> <div className="px-4 py-5 my-5 text-center">
<h1 className="my-5 d-none d-sm-block">Greatest Common Factor Practice</h1> <h1 className="my-5 d-none d-sm-block">Greatest Common Factor Practice</h1>

View File

@@ -46,7 +46,7 @@ function Navbar({maxNum, onMaxNumChanged} : NavbarSettings) {
<div className="nav-item ms-auto me-6 g-3 row align-items-center"> <div className="nav-item ms-auto me-6 g-3 row align-items-center">
<label className="col-form-label col-sm-4 col-form-label-sm ge-2 flex-shrink-0" for="maxNum">Max:</label> <label className="col-form-label col-sm-4 col-form-label-sm ge-2 flex-shrink-0" for="maxNum">Max:</label>
<div className="col-sm-4 flex-shrink-0"> <div className="col-sm-4 flex-shrink-0">
<input className="form-range" type="range" id="maxNum" step={10} value={maxNum} max={200} min={50} onInput={handleSliderInput} onChange={onSliderChange} /> <input className="form-range" type="range" id="maxNum" step={10} value={displayedMax} max={200} min={50} onMouseUp={handleSliderInput} onKeyUp={handleSliderInput} onChange={onSliderChange} />
</div> </div>
<label className="col-form-label col-sm-2 ms-auto">{displayedMax}</label> <label className="col-form-label col-sm-2 ms-auto">{displayedMax}</label>
</div> </div>