`;
});
}
// Helpers
function gcd(a,b){ return b===0?a:gcd(b,a%b);}
function lcm(a,b){ return a*b/gcd(a,b);}
function improper(whole,num,den){ return whole*den + num; }
function toMixed(num,den){
const w = Math.floor(num/den);
const n = num%den;
return [w,n,den];
}
function reduce(num,den){
let g=gcd(Math.abs(num),den);
return [num/g,den/g];
}
// Parse user input mixed fraction like "4/4/5"
function parseMixedInput(str){
const parts=str.split('/');
if(parts.length===3){
const w=Number(parts[0]);
const n=Number(parts[1]);
const d=Number(parts[2]);
return w*d + n; // improper numerator
}else if(parts.length===2){
return Number(parts[0]); // assume simple fraction
}
return NaN;
}
function check(i){
const ex=exercises[i];
let ans=document.getElementById(`ans${i}`).value.trim();
let ansNum=parseMixedInput(ans);
let correctNum, correctDen;
if(ex.type==="frac"){
let n1=parseInt(ex.q[0]), d1=parseInt(ex.q[1]);
let n2=parseInt(ex.q[3]), d2=parseInt(ex.q[4]);
let common=lcm(d1,d2);
correctNum = n1*(common/d1) - n2*(common/d2);
correctDen = common;
}else{
let w1=parseInt(ex.q[0]), n1=parseInt(ex.q[1]), d1=parseInt(ex.q[2]);
let w2=parseInt(ex.q[4]), n2=parseInt(ex.q[5]), d2=parseInt(ex.q[6]);
let i1=improper(w1,n1,d1), i2=improper(w2,n2,d2);
let common=lcm(d1,d2);
correctNum = i1*(common/d1) - i2*(common/d2);
correctDen = common;
}
[correctNum, correctDen] = reduce(correctNum, correctDen);
let [cw, cn, cd] = toMixed(correctNum, correctDen); // mixed fraction
const fb=document.getElementById(`fb${i}`);
if(ansNum === correctNum){
fb.textContent=`✅ Correct! Answer: ${cw} ${cn}/${cd}`;
fb.className="feedback correct";
}else{
fb.textContent=`❌ Wrong. Correct: ${cw} ${cn}/${cd}`;
fb.className="feedback wrong";
}
}
function showWorking(i){
const ex=exercises[i];
let work='', correctNum, correctDen;
if(ex.type==="frac"){
let n1=parseInt(ex.q[0]), d1=parseInt(ex.q[1]);
let n2=parseInt(ex.q[3]), d2=parseInt(ex.q[4]);
let common=lcm(d1,d2);
let n1adj=n1*(common/d1), n2adj=n2*(common/d2);
correctNum = n1adj - n2adj;
correctDen = common;
[correctNum, correctDen] = reduce(correctNum, correctDen);
let [cw,cn,cd]=toMixed(correctNum,correctDen);
work=`Step 1: LCM of denominators: ${d1}, ${d2} → ${common}
Step 2: Adjust numerators: ${n1}×(${common}/${d1})=${n1adj}, ${n2}×(${common}/${d2})=${n2adj}
Step 3: Subtract: ${n1adj}-${n2adj}=${correctNum}/${correctDen} (simplified)
Step 4: Mixed fraction: ${cw} ${cn}/${cd}`;
}else{
let w1=parseInt(ex.q[0]), n1=parseInt(ex.q[1]), d1=parseInt(ex.q[2]);
let w2=parseInt(ex.q[4]), n2=parseInt(ex.q[5]), d2=parseInt(ex.q[6]);
let i1=improper(w1,n1,d1), i2=improper(w2,n2,d2);
let common=lcm(d1,d2);
let n1adj=i1*(common/d1), n2adj=i2*(common/d2);
correctNum = n1adj - n2adj;
correctDen = common;
[correctNum, correctDen] = reduce(correctNum, correctDen);
let [cw,cn,cd]=toMixed(correctNum,correctDen);
work=`Step 1: Convert to improper fractions: ${i1}/${d1} - ${i2}/${d2}
Step 2: LCM: ${common}
Step 3: Adjust numerators: ${n1adj} - ${n2adj}
Step 4: Simplify: ${correctNum}/${correctDen}
Step 5: Mixed fraction: ${cw} ${cn}/${cd}`;
}
document.getElementById(`work${i}`).innerHTML=work;
}
displayQuestions();