From e0f1a24842c2c84d091c3da19c011a08ca59cd13 Mon Sep 17 00:00:00 2001 From: Jonah Paten Date: Tue, 6 Jun 2023 16:16:21 -0700 Subject: [PATCH 1/2] Implemented validation for GraphInput #53 --- src/components/shared/ExerciseSide.tsx | 10 +- .../shared/Exercises/GraphInput.tsx | 116 ++++++++++++------ 2 files changed, 83 insertions(+), 43 deletions(-) diff --git a/src/components/shared/ExerciseSide.tsx b/src/components/shared/ExerciseSide.tsx index 3a0be03..9e3e9e6 100644 --- a/src/components/shared/ExerciseSide.tsx +++ b/src/components/shared/ExerciseSide.tsx @@ -50,30 +50,30 @@ function ExerciseSide({ incrementExercise }: ExerciseSideProps): JSX.Element { { textArray: [ { type: 'text', text: 'turtle.goto(' }, - { type: 'input', width: 2 }, + { type: 'input', width: 2, id: 0, answer: '1' }, { type: 'text', text: ', -1)' }, ], }, { textArray: [ { type: 'text', text: 'turtle.setheading(' }, - { type: 'input', width: 4 }, + { type: 'input', width: 4, id: 1 }, { type: 'text', text: ')' }, ], }, { textArray: [ { type: 'text', text: 'turtle.' }, - { type: 'input', width: 8 }, + { type: 'input', width: 8, id: 2 }, { type: 'text', text: '()' }, ], }, { textArray: [ { type: 'text', text: 'turtle.goto(' }, - { type: 'input', width: 2 }, + { type: 'input', width: 2, id: 3 }, { type: 'text', text: ', ' }, - { type: 'input', width: 2 }, + { type: 'input', width: 2, id: 4 }, { type: 'text', text: ')' }, ], }, diff --git a/src/components/shared/Exercises/GraphInput.tsx b/src/components/shared/Exercises/GraphInput.tsx index f944508..40de469 100644 --- a/src/components/shared/Exercises/GraphInput.tsx +++ b/src/components/shared/Exercises/GraphInput.tsx @@ -1,49 +1,76 @@ +//import { useState } from 'react'; import '../../../styles/Exercises/GraphInput.scss'; -interface GraphQuestionElement { +interface GraphQuestionData { type: string; text?: string; width?: number; answer?: string; + id?: number; } -interface GraphQuestion { - textArray: GraphQuestionElement[]; +interface GraphQuestionGrouping { + questionData: GraphQuestionData; + setCorrect: (id: number, value: boolean) => void; +} + +interface GraphLineGrouping { + data: GraphLineData; + setCorrect: (id: number, value: boolean) => void; } interface GraphInputProps { - questionArray: GraphQuestion[]; + questionArray: GraphLineData[]; nextExercise: () => void; } -function GraphStringElement({ type, text, width }: GraphQuestionElement) { - if (type == 'text') { - return

{text}

; +interface GraphLineData { + textArray: GraphQuestionData[]; +} + +function GraphStringElement({ + questionData, + setCorrect, +}: GraphQuestionGrouping) { + if (questionData.type == 'text') { + return

{questionData?.text ?? ''}

; } else { + //const [inputText, setInputText] = useState(""); + const handleChange = (event: { target: { value: string } }) => { + console.log(`input text: ${event.target.value}`); + console.log(questionData?.answer ?? ''); + if (event.target.value == (questionData?.answer ?? '')) { + console.log('Correct!'); + setCorrect(questionData?.id ?? -1, true); + } else { + setCorrect(questionData?.id ?? -1, false); + } + //setInputText(event.target.value); + }; return ( - +
+ +
); } } -function GraphLine({ textArray }: GraphQuestion) { +function GraphLine({ data, setCorrect }: GraphLineGrouping) { //Map all elements of TextArray - const makeElement = (elementData: GraphQuestionElement): JSX.Element => { + const makeElement = (elementData: GraphQuestionData): JSX.Element => { return ( - + ); }; return ( -
{textArray.map(makeElement)}
+
+ {data.textArray.map(makeElement)} +
); /*return (
@@ -58,32 +85,45 @@ function GraphInput({ questionArray, nextExercise, }: GraphInputProps): JSX.Element { - const makeLine = (lineArray: GraphQuestion): JSX.Element => { - return ; + const valueMap = new Map(); + //const [wrong, setWrong] = useState(false); + const setValueCorrect = (id: number, value: boolean): void => { + console.log(`Setting ${id} to ${value}`); + valueMap.set(id, value); + }; + + const checkCorrect = (): void => { + console.log('Checking correct'); + // @ts-ignore + for (const i of valueMap.values()) { + console.log(i); + if (!i) { + console.log(`${i} is incorrect`); + //setWrong(true); + return; + } + nextExercise(); + } + }; + + const makeLine = ( + data: GraphLineData, + setCorrect: (id: number, value: boolean) => void + ): JSX.Element => { + return ; }; return (
-
{questionArray.map(makeLine)}
+
+ {questionArray.map((x) => makeLine(x, setValueCorrect))} +
-
); - /* - return ( -
-
- -
-
- );*/ } export default GraphInput; From f88a969e6ada3784ce5e33a10dc4fc54529e07b3 Mon Sep 17 00:00:00 2001 From: Jonah Paten Date: Tue, 17 Oct 2023 16:13:36 -0700 Subject: [PATCH 2/2] Fixed map initialization and added more answers #53 --- src/components/shared/ExerciseSide.tsx | 18 +++++++++++------- src/components/shared/Exercises/GraphInput.tsx | 12 ++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/components/shared/ExerciseSide.tsx b/src/components/shared/ExerciseSide.tsx index 9e3e9e6..5d425eb 100644 --- a/src/components/shared/ExerciseSide.tsx +++ b/src/components/shared/ExerciseSide.tsx @@ -17,7 +17,7 @@ function ExerciseSide({ incrementExercise }: ExerciseSideProps): JSX.Element { const [completeExercises, setCompleteExercises] = useState(0); type availableExercises = 'axis' | 'congrats' | 'circle' | 'graph'; - const exercises: availableExercises[] = ['axis', 'graph', 'congrats']; + const exercises: availableExercises[] = ['graph', 'congrats']; let curExercise; if (exercises[completeExercises] == 'graph') { @@ -50,35 +50,39 @@ function ExerciseSide({ incrementExercise }: ExerciseSideProps): JSX.Element { { textArray: [ { type: 'text', text: 'turtle.goto(' }, - { type: 'input', width: 2, id: 0, answer: '1' }, + { type: 'input', width: 2, id: 0, answer: '0' }, { type: 'text', text: ', -1)' }, ], }, { textArray: [ { type: 'text', text: 'turtle.setheading(' }, - { type: 'input', width: 4, id: 1 }, + { type: 'input', width: 4, id: 1, answer: '2' }, { type: 'text', text: ')' }, ], }, { textArray: [ { type: 'text', text: 'turtle.' }, - { type: 'input', width: 8, id: 2 }, + { type: 'input', width: 8, id: 2, answer: '4' }, { type: 'text', text: '()' }, ], }, { textArray: [ { type: 'text', text: 'turtle.goto(' }, - { type: 'input', width: 2, id: 3 }, + { type: 'input', width: 2, id: 3, answer: '6' }, { type: 'text', text: ', ' }, - { type: 'input', width: 2, id: 4 }, + { type: 'input', width: 2, id: 4, answer: '8' }, { type: 'text', text: ')' }, ], }, ]} - nextExercise={() => incrementExercise()} + nextExercise={() => { + setCompleteExercises(completeExercises + 1); + incrementExercise(); + return; + }} />
diff --git a/src/components/shared/Exercises/GraphInput.tsx b/src/components/shared/Exercises/GraphInput.tsx index 40de469..f26bb52 100644 --- a/src/components/shared/Exercises/GraphInput.tsx +++ b/src/components/shared/Exercises/GraphInput.tsx @@ -1,4 +1,5 @@ //import { useState } from 'react'; +//import { useState } from 'react'; import '../../../styles/Exercises/GraphInput.scss'; interface GraphQuestionData { @@ -86,7 +87,13 @@ function GraphInput({ nextExercise, }: GraphInputProps): JSX.Element { const valueMap = new Map(); - //const [wrong, setWrong] = useState(false); + for (const question of questionArray) { + for (const item of question.textArray) { + if (item.type == 'input') { + valueMap.set(item.id ?? -1, false); + } + } + } const setValueCorrect = (id: number, value: boolean): void => { console.log(`Setting ${id} to ${value}`); valueMap.set(id, value); @@ -102,8 +109,9 @@ function GraphInput({ //setWrong(true); return; } - nextExercise(); } + console.log('all right!'); + nextExercise(); }; const makeLine = (