// components/practice.js
// Interactive practice with hints and feedback
let currentPractice = {
concept: null,
questions: [],
currentIndex: 0,
score: 0,
mode: 'guided' // 'guided' or 'independent'
};
window.renderPractice = function(conceptId) {
const content = document.getElementById('content');
const concept = conceptId ? window.concepts.find(c => c.id === conceptId) : window.concepts[0];
if (!concept) {
content.innerHTML = '
No practice available.
';
return;
}
currentPractice.concept = concept;
currentPractice.questions = concept.practiceQuestions || [];
currentPractice.currentIndex = 0;
currentPractice.score = 0;
content.innerHTML = `
${currentPractice.questions.map((_, idx) => `
`).join('')}
${currentPractice.questions[0]?.difficulty || 'easy'}
${currentPractice.questions[0]?.q || '23 × 54 = ?'}
${currentPractice.mode === 'guided' ? `
💡 Hint 1: Start with vertical multiplication
💡 Hint 2: Crosswise addition
💡 Hint 3: Final vertical
` : '
No hints in test mode! Do your best! 💪
'}
Check Answer
Question
1/${currentPractice.questions.length}
Score
0
`;
if (window.lucide) lucide.createIcons();
};
window.setMode = function(mode) {
currentPractice.mode = mode;
renderPractice(currentPractice.concept.id);
};
window.showHint = function(level) {
const hints = {
1: "Multiply the left digits vertically (tens place)",
2: "Cross multiply and add (tens×units + units×tens)",
3: "Multiply right digits vertically (units place) and add any carry"
};
const btn = document.getElementById(`hint-${level}`);
if (btn) {
btn.textContent = hints[level];
btn.classList.remove('hidden');
btn.disabled = true;
}
// Show next hint button
if (level < 3) {
const nextBtn = document.getElementById(`hint-${level + 1}`);
if (nextBtn) nextBtn.classList.remove('hidden');
}
};
window.checkAnswer = function() {
const input = document.getElementById('answer-input');
const feedback = document.getElementById('feedback');
const currentQ = currentPractice.questions[currentPractice.currentIndex];
const userAns = parseInt(input.value);
if (isNaN(userAns)) return;
const isCorrect = userAns === currentQ.a;
feedback.classList.remove('hidden');
if (isCorrect) {
currentPractice.score++;
feedback.innerHTML = `
Correct! 🎉
${currentQ.a} is right!
`;
updateProgress(true);
} else {
feedback.innerHTML = `
Not quite
The answer is ${currentQ.a}. Let's review the steps!
Review Concept
`;
}
document.getElementById('score-display').textContent = currentPractice.score;
if (window.lucide) lucide.createIcons();
};
window.nextQuestion = function() {
currentPractice.currentIndex++;
if (currentPractice.currentIndex >= currentPractice.questions.length) {
showPracticeComplete();
} else {
// Reload with next question
const content = document.getElementById('content');
const q = currentPractice.questions[currentPractice.currentIndex];
// Update UI for next question
document.querySelector('.question-text').textContent = q.q;
document.querySelector('.difficulty-badge').className = `difficulty-badge ${q.difficulty}`;
document.querySelector('.difficulty-badge').textContent = q.difficulty;
document.getElementById('answer-input').value = '';
document.getElementById('feedback').classList.add('hidden');
// Update progress dots
document.querySelectorAll('.progress-dot').forEach((dot, idx) => {
dot.classList.toggle('active', idx === currentPractice.currentIndex);
dot.classList.toggle('completed', idx < currentPractice.currentIndex);
});
// Update stats
document.querySelector('.stat-box strong').textContent =
`${currentPractice.currentIndex + 1}/${currentPractice.questions.length}`;
}
};
function showPracticeComplete() {
const content = document.querySelector('.practice-container');
content.innerHTML = `
🎊
Practice Complete!
You scored ${currentPractice.score}/${currentPractice.questions.length}
Learn More
Review
`;
// Mark concept as completed if score is good
if (currentPractice.score >= currentPractice.questions.length * 0.7) {
if (!state.conceptsCompleted.includes(currentPractice.concept.id)) {
state.conceptsCompleted.push(currentPractice.concept.id);
}
}
}
function updateProgress(success) {
// Update global state
state.lastActivity = Date.now();
if (success && state.weekProgress < 100) {
state.weekProgress = Math.min(100, state.weekProgress + 10);
}
}