Course Finder & Grammar Tutor

Learning Assistant

Find courses or improve your writing with a grammar tutor.

Find Open-Source Course Materials

Enter a topic to find free course materials.

Spelling & Grammar Tutor

Your personalized grammar and spelling lesson will appear here.

${res.description}

`; courseResultsContainer.appendChild(card); }); }; /** * Fetches and displays a grammar and spelling lesson. */ const handleTutorAnalysis = async () => { const text = tutorInput.value.trim(); if (!text) { showMessage('Please enter some text to analyze.'); return; } clearMessage(); setLoadingState(tutorBtn, tutorBtnText, tutorBtnLoader, true); tutorResultsContainer.innerHTML = '

Analyzing your text...

'; const prompt = `Act as a friendly and encouraging spelling and grammar tutor. Analyze the following text for errors. For each error, provide a mini-lesson that includes: 1) The type of error (e.g., Spelling, Subject-Verb Agreement, Punctuation). 2) The original incorrect phrase. 3) The corrected phrase. 4) A simple, clear explanation of the rule. Text: "${text}"`; const payload = { contents: [{ parts: [{ text: prompt }] }], generationConfig: { responseMimeType: "application/json", responseSchema: { type: "OBJECT", properties: { lesson_points: { type: "ARRAY", items: { type: "OBJECT", properties: { error_type: { type: "STRING" }, original_phrase: { type: "STRING" }, corrected_phrase: { type: "STRING" }, explanation: { type: "STRING" } }, required: ["error_type", "original_phrase", "corrected_phrase", "explanation"] } } }, required: ["lesson_points"] } } }; try { const apiKey = ""; const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`; const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) throw new Error(`API Error: ${response.statusText}`); const result = await response.json(); const contentPart = result.candidates?.[0]?.content?.parts?.[0]; if (contentPart?.text) { const parsedResult = JSON.parse(contentPart.text); displayTutorResults(parsedResult.lesson_points); } else { throw new Error("Invalid response structure from API."); } } catch (error) { console.error("Error during tutor analysis:", error); showMessage(`An error occurred: ${error.message}.`); tutorResultsContainer.innerHTML = '

Could not analyze the text. Please try again.

'; } finally { setLoadingState(tutorBtn, tutorBtnText, tutorBtnLoader, false); } }; const displayTutorResults = (lessons) => { tutorResultsContainer.innerHTML = ''; if (!lessons || lessons.length === 0) { tutorResultsContainer.innerHTML = '

Great job!

No errors were found in your text.

'; return; } lessons.forEach(lesson => { const card = document.createElement('div'); card.className = 'bg-white p-5 rounded-lg shadow border'; card.innerHTML = `

${lesson.error_type}

${lesson.original_phrase} ${lesson.corrected_phrase}

Why it changed:

${lesson.explanation}

`; tutorResultsContainer.appendChild(card); }); }; // --- Event Listeners --- findCourseBtn.addEventListener('click', handleCourseMaterialSearch); courseTopicInput.addEventListener('keyup', (event) => { if (event.key === 'Enter') { event.preventDefault(); findCourseBtn.click(); } }); tutorBtn.addEventListener('click', handleTutorAnalysis); // --- Initial Load --- showTab('courseFinderTab'); });
Scroll to Top