From a0406bd8497c83fb42d7a4b5e037e55beb055b12 Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 10 Apr 2020 13:17:14 -0600 Subject: [PATCH] add grading algorithm for cloze drag and drop --- src/grader.ts | 24 ++++++++++++++++++++++++ src/types.ts | 1 + test/grader.spec.ts | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/grader.ts b/src/grader.ts index d5feece..7ffc280 100644 --- a/src/grader.ts +++ b/src/grader.ts @@ -16,6 +16,30 @@ const grader = (question: Question, choices: Array>, response: Res return (currentChosenChoice && currentChosenChoice.correct) ? 1.0 : 0.0; }).reduce((sum, resp) => (sum += resp), 0); return correctResponses / choices.length; + } else if (question.questionType === "CLOZE_DRAG_AND_DROP") { + let ccio = choices[0].sort((a, b) => a.correctOrder - b.correctOrder); + let ccioKeys = ccio.map((cc) => cc.key); + let exactSameOrderKeys = true; + for (let i = 0; i < ccioKeys.length; i++) { + if (ccioKeys[i] !== response.keys[i]) { + exactSameOrderKeys = false; + } + } + if (exactSameOrderKeys) { + return 1.0; + } else { + let allKeysPresent = true; + for (let k of response.keys) { + if (!ccioKeys.includes(k)) { + allKeysPresent = false; + } + } + if (allKeysPresent) { + return 0.5; + } else { + return 0.0; + } + } } else { return 0.0; } diff --git a/src/types.ts b/src/types.ts index 9a6c44d..694606b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,7 @@ export interface Choice { readonly key: string, readonly text: string, readonly correct: boolean, + correctOrder?: number, } export interface Response { diff --git a/test/grader.spec.ts b/test/grader.spec.ts index 211ab66..1078207 100644 --- a/test/grader.spec.ts +++ b/test/grader.spec.ts @@ -73,4 +73,23 @@ describe("grader", () => { expect(grader(question, choices, { keys: [ "A", "C" ] })).toEqual(0.5) }); }); + + describe("CLOZE_DRAG_AND_DROP", () => { + const question = { + questionType: "CLOZE_DRAG_AND_DROP", + prompt: "For {target1}, then {target2}", + }; + const choices = [[ + { key: "A", text: "A", correct: true, correctOrder: 1 }, + { key: "B", text: "B", correct: true, correctOrder: 2 }, + ]]; + + it("should return 1.0 if responses match correct choices in order", () => { + expect(grader(question, choices, { keys: ["A", "B"] })).toEqual(1.0); + }); + + it("should return 0.5 if responses are out of order", () => { + expect(grader(question, choices, { keys: ["B", "A"] })).toEqual(0.5); + }); + }); });