diff --git a/.eslintignore b/.eslintignore
new file mode 100644
index 0000000000..9f1cb867ce
--- /dev/null
+++ b/.eslintignore
@@ -0,0 +1,2 @@
+**/*.test.tsx
+**/*.test.ts
diff --git a/.eslintrc.json b/.eslintrc.json
new file mode 100644
index 0000000000..97fe8dbcbd
--- /dev/null
+++ b/.eslintrc.json
@@ -0,0 +1,12 @@
+{
+ "extends": ["react-app", "react-app/jest"],
+ "rules": {},
+ "overrides": [
+ {
+ "files": ["**/*.test.ts", "**/*.test.tsx"],
+ "rules": {
+ "testing-library/no-render-in-setup": "off"
+ }
+ }
+ ]
+}
diff --git a/package.json b/package.json
index cf6e1bc772..1fee0f8e57 100644
--- a/package.json
+++ b/package.json
@@ -21,7 +21,7 @@
"test": "react-scripts test",
"test:cov": "react-scripts test --coverage --watchAll",
"eject": "react-scripts eject",
- "lint": "eslint ./src --ext .tsx --ext .ts --max-warnings 0",
+ "lint": "eslint ./src --ext .tsx --ext .ts --max-warnings 0 --ignore-pattern \"*.test.ts\" --ignore-pattern \"*.test.tsx\"",
"eslint-output": "eslint-output ./src --ext .tsx --ext .ts --max-warnings 0",
"format": "prettier --config .prettierrc --write src/**/*.{ts,tsx}"
},
diff --git a/public/tasks/task-nested.md b/public/tasks/task-nested.md
new file mode 100644
index 0000000000..6d29f9369f
--- /dev/null
+++ b/public/tasks/task-nested.md
@@ -0,0 +1,5 @@
+# Task - Nested
+
+Version: 0.0.1
+
+Implement functions that work with nested arrays and objects immutably.
diff --git a/public/tasks/task-objects.md b/public/tasks/task-objects.md
new file mode 100644
index 0000000000..480889da0d
--- /dev/null
+++ b/public/tasks/task-objects.md
@@ -0,0 +1,5 @@
+# Task - Objects
+
+Version: 0.0.1
+
+Implement functions that work with objects immutably.
diff --git a/src/App.css b/src/App.css
index ad32fac073..90841ec74e 100644
--- a/src/App.css
+++ b/src/App.css
@@ -15,7 +15,7 @@
.App-header {
width: 100%;
- background-color: #282c34;
+ background-color: orange;
min-height: 40vh;
display: flex;
flex-direction: column;
diff --git a/src/App.tsx b/src/App.tsx
index b77558eaac..c6a8037756 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,16 +1,89 @@
import React from "react";
import "./App.css";
+import { Button, Container, Row, Col } from "react-bootstrap";
+import { Counter } from "./components/Counter";
+import { RevealAnswer } from "./components/RevealAnswer";
+import { ChangeType } from "./components/ChangeType";
+import { StartAttempt } from "./components/StartAttempt";
+import { TwoDice } from "./components/TwoDice";
+import { CycleHoliday } from "./components/CycleHoliday";
function App(): React.JSX.Element {
return (
- UD CISC275 with React Hooks and TypeScript
+ UD CISC275 with React Hooks and TypeScript Luke Remmler Hello
+ World
Edit src/App.tsx and save. This page will
automatically reload.
+
Luke Remmler App
+
+ Unordered List:
+
+ First thing
+ Another thing
+ A third item
+
+
{
+ console.log("Hello World!");
+ }}
+ >
+ Log Hello World
+
+
+
+
+
+
+
+
+
+
+
+
+ {/* Added Components */}
+
+
Counter Component
+
+
+
+
Reveal Answer Component
+
+
+
+
Change Type Component
+
+
+
+
Start Attempt Component
+
+
+
+
Two Dice Component
+
+
+
+
Cycle Holiday Component
+
);
}
diff --git a/src/arrays.ts b/src/arrays.ts
new file mode 100644
index 0000000000..f9cf3e21d0
--- /dev/null
+++ b/src/arrays.ts
@@ -0,0 +1,136 @@
+/**
+ * Consume an array of numbers, and return a new array containing
+ * JUST the first and last number. If there are no elements, return
+ * an empty array. If there is one element, the resulting list should
+ * the number twice.
+ */
+export function bookEndList(numbers: number[]): number[] {
+ if (numbers.length === 0) {
+ return [];
+ }
+ if (numbers.length === 1) {
+ return [numbers[0], numbers[0]];
+ }
+ return [numbers[0], numbers[numbers.length - 1]];
+}
+
+/**
+ * Consume an array of numbers, and return a new array where each
+ * number has been tripled (multiplied by 3).
+ */
+export function tripleNumbers(numbers: number[]): number[] {
+ return numbers.map((num) => num * 3);
+}
+
+/**
+ * Consume an array of strings and convert them to integers. If
+ * the number cannot be parsed as an integer, convert it to 0 instead.
+ */
+export function stringsToIntegers(numbers: string[]): number[] {
+ return numbers.map((str) => {
+ const parsed = parseInt(str, 10);
+ return isNaN(parsed) ? 0 : parsed;
+ });
+}
+
+/**
+ * Consume an array of strings and return them as numbers. Note that
+ * the strings MAY have "$" symbols at the beginning, in which case
+ * those should be removed. If the result cannot be parsed as an integer,
+ * convert it to 0 instead.
+ */
+// Remember, you can write functions as lambdas too! They work exactly the same.
+export const removeDollars = (amounts: string[]): number[] => {
+ return amounts.map((amount) => {
+ // Remove $ symbol if present
+ const cleanAmount =
+ amount.startsWith("$") ? amount.substring(1) : amount;
+ const parsed = parseInt(cleanAmount, 10);
+ return isNaN(parsed) ? 0 : parsed;
+ });
+};
+
+/**
+ * Consume an array of messages and return a new list of the messages. However, any
+ * string that ends in "!" should be made uppercase. Also, remove any strings that end
+ * in question marks ("?").
+ */
+export const shoutIfExclaiming = (messages: string[]): string[] => {
+ return messages
+ .filter((message) => !message.endsWith("?"))
+ .map((message) =>
+ message.endsWith("!") ? message.toUpperCase() : message,
+ );
+};
+
+/**
+ * Consumes an array of words and returns the number of words that are LESS THAN
+ * 4 letters long.
+ */
+export function countShortWords(words: string[]): number {
+ return words.filter((word) => word.length < 4).length;
+}
+
+/**
+ * Consumes an array of colors (e.g., 'red', 'purple') and returns true if ALL
+ * the colors are either 'red', 'blue', or 'green'. If an empty list is given,
+ * then return true.
+ */
+export function allRGB(colors: string[]): boolean {
+ if (colors.length === 0) return true;
+ return colors.every(
+ (color) => color === "red" || color === "blue" || color === "green",
+ );
+}
+
+/**
+ * Consumes an array of numbers, and produces a string representation of the
+ * numbers being added together along with their actual sum.
+ *
+ * For instance, the array [1, 2, 3] would become "6=1+2+3".
+ * And the array [] would become "0=0".
+ */
+export function makeMath(addends: number[]): string {
+ if (addends.length === 0) {
+ return "0=0";
+ }
+
+ const sum = addends.reduce((total, num) => total + num, 0);
+ const equation = addends.join("+");
+
+ return `${sum}=${equation}`;
+}
+
+/**
+ * Consumes an array of numbers and produces a new array of the same numbers,
+ * with one difference. After the FIRST negative number, insert the sum of all
+ * previous numbers in the list. If there are no negative numbers, then append
+ * the sum to the list.
+ *
+ * For instance, the array [1, 9, -5, 7] would become [1, 9, -5, 10, 7]
+ * And the array [1, 9, 7] would become [1, 9, 7, 17]
+ */
+export function injectPositive(values: number[]): number[] {
+ // Find the index of the first negative number
+ const firstNegativeIndex = values.findIndex((num) => num < 0);
+
+ if (firstNegativeIndex === -1) {
+ // No negative numbers found, append sum to the end
+ const sum = values.reduce((total, num) => total + num, 0);
+ return [...values, sum];
+ } else {
+ // Calculate sum of numbers before the first negative
+ const numbersBeforeNegative = values.slice(0, firstNegativeIndex);
+ const sum = numbersBeforeNegative.reduce(
+ (total, num) => total + num,
+ 0,
+ );
+
+ // Insert the sum after the first negative number
+ return [
+ ...values.slice(0, firstNegativeIndex + 1),
+ sum,
+ ...values.slice(firstNegativeIndex + 1),
+ ];
+ }
+}
diff --git a/src/components/ChangeType.tsx b/src/components/ChangeType.tsx
new file mode 100644
index 0000000000..34cbc395cb
--- /dev/null
+++ b/src/components/ChangeType.tsx
@@ -0,0 +1,15 @@
+import React, { useState } from "react";
+
+export function ChangeType(): JSX.Element {
+ const [type, setType] = useState("Short Answer");
+ return (
+
+ setType(
+ type === "Multiple Choice" ? "Short Answer" : "Multiple Choice"
+ )}>
+ Change Type
+
+ {type === "Multiple Choice" ? "Multiple Choice" : "Short Answer"}
+
+ );
+}
diff --git a/src/components/Counter.tsx b/src/components/Counter.tsx
new file mode 100644
index 0000000000..e3ca09eb3e
--- /dev/null
+++ b/src/components/Counter.tsx
@@ -0,0 +1,12 @@
+import React, { useState } from "react";
+
+export function Counter(): JSX.Element {
+ const [value, setValue] = useState(0);
+ return (
+
+
setValue(value + 1)}>Add One
+
Count: {value}
+
setValue(value - 1)}>Subtract One
+
+ );
+}
diff --git a/src/components/CycleHoliday.tsx b/src/components/CycleHoliday.tsx
new file mode 100644
index 0000000000..c8c3c1ed3d
--- /dev/null
+++ b/src/components/CycleHoliday.tsx
@@ -0,0 +1,34 @@
+import React, { useState } from "react";
+
+export function CycleHoliday(): JSX.Element {
+ type Holiday = "🎁" | "🐇" | "🎃" | "🦃" | "🎄";
+ const [holiday, setHoliday] = useState("🎁");
+
+ const byAlphabet: Record = {
+ "🎁": "🐇",
+ "🐇": "🎃",
+ "🎃": "🦃",
+ "🦃": "🎄",
+ "🎄": "🎁"
+ };
+
+ const byYear: Record = {
+ "🎁": "🐇",
+ "🐇": "🎃",
+ "🎃": "🦃",
+ "🦃": "🎄",
+ "🎄": "🎁"
+ };
+
+ return (
+
+
Holiday: {holiday}
+
setHoliday(byAlphabet[holiday])}>
+ Advance by Alphabet
+
+
setHoliday(byYear[holiday])}>
+ Advance by Year
+
+
+ );
+}
diff --git a/src/components/RevealAnswer.tsx b/src/components/RevealAnswer.tsx
new file mode 100644
index 0000000000..86bbfd9bc7
--- /dev/null
+++ b/src/components/RevealAnswer.tsx
@@ -0,0 +1,11 @@
+import React, { useState } from "react";
+
+export function RevealAnswer(): JSX.Element {
+ const [visible, setVisible] = useState(false);
+ return (
+
+
setVisible(!visible)}>Reveal Answer
+ {visible &&
42
}
+
+ );
+}
diff --git a/src/components/StartAttempt.tsx b/src/components/StartAttempt.tsx
new file mode 100644
index 0000000000..65f902d49c
--- /dev/null
+++ b/src/components/StartAttempt.tsx
@@ -0,0 +1,23 @@
+import React, { useState } from "react";
+
+export function StartAttempt(): JSX.Element {
+ const [attempts, setAttempts] = useState(4);
+ const [progress, setProgress] = useState(false);
+ return (
+
+
{attempts}
+
{setProgress(true); setAttempts(attempts - 1);}}
+ disabled={progress || attempts === 0}
+ >
+ Start Quiz
+
+
setProgress(false)} disabled={!progress}>
+ Stop Quiz
+
+
setAttempts(attempts + 1)} disabled={progress}>
+ Mulligan
+
+
+ );
+}
diff --git a/src/components/TwoDice.tsx b/src/components/TwoDice.tsx
new file mode 100644
index 0000000000..295e34d2e3
--- /dev/null
+++ b/src/components/TwoDice.tsx
@@ -0,0 +1,22 @@
+import React, { useState } from "react";
+
+export function TwoDice(): JSX.Element {
+ const [left, setLeft] = useState(1);
+ const [right, setRight] = useState(2);
+ return (
+
+
+ {left}
+ {right}
+
+
setLeft(Math.floor(Math.random() * 6) + 1)}>
+ Roll Left
+
+
setRight(Math.floor(Math.random() * 6) + 1)}>
+ Roll Right
+
+ {left === right && left === 1 &&
Lose
}
+ {left === right && left !== 1 &&
Win
}
+
+ );
+}
diff --git a/src/data/questions.json b/src/data/questions.json
new file mode 100644
index 0000000000..0411f30afe
--- /dev/null
+++ b/src/data/questions.json
@@ -0,0 +1,220 @@
+{
+ "BLANK_QUESTIONS": [
+ {
+ "id": 1,
+ "name": "Question 1",
+ "body": "",
+ "type": "multiple_choice_question",
+ "options": [],
+ "expected": "",
+ "points": 1,
+ "published": false
+ },
+ {
+ "id": 47,
+ "name": "My New Question",
+ "body": "",
+ "type": "multiple_choice_question",
+ "options": [],
+ "expected": "",
+ "points": 1,
+ "published": false
+ },
+ {
+ "id": 2,
+ "name": "Question 2",
+ "body": "",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "",
+ "points": 1,
+ "published": false
+ }
+ ],
+ "SIMPLE_QUESTIONS": [
+ {
+ "id": 1,
+ "name": "Addition",
+ "body": "What is 2+2?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "4",
+ "points": 1,
+ "published": true
+ },
+ {
+ "id": 2,
+ "name": "Letters",
+ "body": "What is the last letter of the English alphabet?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "Z",
+ "points": 1,
+ "published": false
+ },
+ {
+ "id": 5,
+ "name": "Colors",
+ "body": "Which of these is a color?",
+ "type": "multiple_choice_question",
+ "options": ["red", "apple", "firetruck"],
+ "expected": "red",
+ "points": 1,
+ "published": true
+ },
+ {
+ "id": 9,
+ "name": "Shapes",
+ "body": "What shape can you make with one line?",
+ "type": "multiple_choice_question",
+ "options": ["square", "triangle", "circle"],
+ "expected": "circle",
+ "points": 2,
+ "published": false
+ }
+ ],
+ "TRIVIA_QUESTIONS": [
+ {
+ "id": 1,
+ "name": "Mascot",
+ "body": "What is the name of the UD Mascot?",
+ "type": "multiple_choice_question",
+ "options": ["Bluey", "YoUDee", "Charles the Wonder Dog"],
+ "expected": "YoUDee",
+ "points": 7,
+ "published": false
+ },
+ {
+ "id": 2,
+ "name": "Motto",
+ "body": "What is the University of Delaware's motto?",
+ "type": "multiple_choice_question",
+ "options": [
+ "Knowledge is the light of the mind",
+ "Just U Do it",
+ "Nothing, what's the motto with you?"
+ ],
+ "expected": "Knowledge is the light of the mind",
+ "points": 3,
+ "published": false
+ },
+ {
+ "id": 3,
+ "name": "Goats",
+ "body": "How many goats are there usually on the Green?",
+ "type": "multiple_choice_question",
+ "options": [
+ "Zero, why would there be goats on the green?",
+ "18420",
+ "Two"
+ ],
+ "expected": "Two",
+ "points": 10,
+ "published": false
+ }
+ ],
+ "EMPTY_QUESTIONS": [
+ {
+ "id": 1,
+ "name": "Empty 1",
+ "body": "This question is not empty, right?",
+ "type": "multiple_choice_question",
+ "options": ["correct", "it is", "not"],
+ "expected": "correct",
+ "points": 5,
+ "published": true
+ },
+ {
+ "id": 2,
+ "name": "Empty 2",
+ "body": "",
+ "type": "multiple_choice_question",
+ "options": ["this", "one", "is", "not", "empty", "either"],
+ "expected": "one",
+ "points": 5,
+ "published": true
+ },
+ {
+ "id": 3,
+ "name": "Empty 3",
+ "body": "This questions is not empty either!",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "",
+ "points": 5,
+ "published": true
+ },
+ {
+ "id": 4,
+ "name": "Empty 4",
+ "body": "",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "Even this one is not empty",
+ "points": 5,
+ "published": true
+ },
+ {
+ "id": 5,
+ "name": "Empty 5 (Actual)",
+ "body": "",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "",
+ "points": 5,
+ "published": false
+ }
+ ],
+ "SIMPLE_QUESTIONS_2": [
+ {
+ "id": 478,
+ "name": "Students",
+ "body": "How many students are taking CISC275 this semester?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "90",
+ "points": 53,
+ "published": true
+ },
+ {
+ "id": 1937,
+ "name": "Importance",
+ "body": "On a scale of 1 to 10, how important is this quiz for them?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "10",
+ "points": 47,
+ "published": true
+ },
+ {
+ "id": 479,
+ "name": "Sentience",
+ "body": "Is it technically possible for this quiz to become sentient?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "Yes",
+ "points": 40,
+ "published": true
+ },
+ {
+ "id": 777,
+ "name": "Danger",
+ "body": "If this quiz became sentient, would it pose a danger to others?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "Yes",
+ "points": 60,
+ "published": true
+ },
+ {
+ "id": 1937,
+ "name": "Listening",
+ "body": "Is this quiz listening to us right now?",
+ "type": "short_answer_question",
+ "options": [],
+ "expected": "Yes",
+ "points": 100,
+ "published": true
+ }
+ ]
+}
diff --git a/src/functions.ts b/src/functions.ts
new file mode 100644
index 0000000000..668480f4cb
--- /dev/null
+++ b/src/functions.ts
@@ -0,0 +1,50 @@
+/**
+ * Consumes a single temperature in Fahrenheit (a number) and converts to Celsius
+ * using this formula:
+ * C = (F - 32) * 5/9
+ */
+export function fahrenheitToCelius(temperature: number): number {
+ return ((temperature - 32) * 5) / 9;
+}
+
+/**
+ * Consumes three numbers and produces their sum. BUT you should only add a number
+ * if the number is greater than zero.
+ */
+export function add3(first: number, second: number, third: number): number {
+ let sum = 0;
+ if (first > 0) sum += first;
+ if (second > 0) sum += second;
+ if (third > 0) sum += third;
+ return sum;
+}
+
+/**
+ * Consumes a string and produces the same string in UPPERCASE and with an exclamation
+ * mark added to the end.
+ */
+export function shout(message: string): string {
+ return message.toUpperCase() + "!";
+}
+
+/**
+ * Consumes a string (a message) and returns a boolean if the string ends in a question
+ * mark. Do not use an `if` statement in solving this question.
+ */
+export function isQuestion(message: string): boolean {
+ return message.endsWith("?");
+}
+
+/**
+ * Consumes a word (a string) and returns either `true`, `false`, or `null`. If the string
+ * is "yes" (upper or lower case), then return `true`. If the string is "no" (again, either
+ * upper or lower case), then return `false`. Otherwise, return `null`.
+ */
+export function convertYesNo(word: string): boolean | null {
+ const lowercase = word.toLowerCase();
+ return (
+ lowercase === "yes" ? true
+ : lowercase === "no" ? false
+ : null
+ );
+}
diff --git a/src/global.d.ts b/src/global.d.ts
new file mode 100644
index 0000000000..9c0f9101af
--- /dev/null
+++ b/src/global.d.ts
@@ -0,0 +1,5 @@
+declare module "*.json" {
+ import { Question } from "./interfaces/question";
+ const value: Question[];
+ export default value;
+}
diff --git a/src/interfaces/answer.ts b/src/interfaces/answer.ts
new file mode 100644
index 0000000000..ef51b34733
--- /dev/null
+++ b/src/interfaces/answer.ts
@@ -0,0 +1,6 @@
+export interface Answer {
+ questionId: number;
+ correct: boolean;
+ text: string;
+ submitted: boolean;
+}
diff --git a/src/interfaces/questions.ts b/src/interfaces/questions.ts
new file mode 100644
index 0000000000..ed2f36ad36
--- /dev/null
+++ b/src/interfaces/questions.ts
@@ -0,0 +1,13 @@
+// interfaces/question.ts
+//export type QuestionType = "multiple_choice_question" | "short_answer_question";
+
+export interface Question {
+ id: number;
+ name: string;
+ type: QuestionType;
+ body: string;
+ expected: string;
+ options: string[];
+ points: number;
+ published: boolean;
+}
diff --git a/src/nested.test.ts b/src/nested.test.ts
new file mode 100644
index 0000000000..572a7a028d
--- /dev/null
+++ b/src/nested.test.ts
@@ -0,0 +1,1238 @@
+import { Question } from "./interfaces/question";
+import {
+ getPublishedQuestions,
+ getNonEmptyQuestions,
+ findQuestion,
+ removeQuestion,
+ getNames,
+ sumPoints,
+ sumPublishedPoints,
+ toCSV,
+ makeAnswers,
+ publishAll,
+ sameType,
+ addNewQuestion,
+ renameQuestionById,
+ changeQuestionTypeById,
+ editOption,
+ duplicateQuestionInArray
+} from "./nested";
+import testQuestionData from "./data/questions.json";
+import backupQuestionData from "./data/questions.json";
+
+const {
+ BLANK_QUESTIONS,
+ SIMPLE_QUESTIONS,
+ TRIVIA_QUESTIONS,
+ EMPTY_QUESTIONS,
+ SIMPLE_QUESTIONS_2
+}: Record =
+ // Typecast the test data that we imported to be a record matching
+ // strings to the question list
+ testQuestionData as Record;
+
+// We have backup versions of the data to make sure all changes are immutable
+const {
+ BLANK_QUESTIONS: BACKUP_BLANK_QUESTIONS,
+ SIMPLE_QUESTIONS: BACKUP_SIMPLE_QUESTIONS,
+ TRIVIA_QUESTIONS: BACKUP_TRIVIA_QUESTIONS,
+ EMPTY_QUESTIONS: BACKUP_EMPTY_QUESTIONS,
+ SIMPLE_QUESTIONS_2: BACKUP_SIMPLE_QUESTIONS_2
+}: Record = backupQuestionData as Record<
+ string,
+ Question[]
+>;
+
+const NEW_BLANK_QUESTION = {
+ id: 142,
+ name: "A new question",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+};
+
+const NEW_TRIVIA_QUESTION = {
+ id: 449,
+ name: "Colors",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ /*body: "The official colors of UD are Blue and ...?",
+ type: "multiple_choice_question",
+ options: ["Black, like my soul", "Blue again, we're tricky.", "#FFD200"],
+ expected: "#FFD200",*/
+ points: 1,
+ published: false
+};
+
+////////////////////////////////////////////
+// Actual tests
+
+describe("Testing the Question[] functions", () => {
+ //////////////////////////////////
+ // getPublishedQuestions
+
+ test("Testing the getPublishedQuestions function", () => {
+ expect(getPublishedQuestions(BLANK_QUESTIONS)).toEqual([]);
+ expect(getPublishedQuestions(SIMPLE_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "firetruck"],
+ expected: "red",
+ points: 1,
+ published: true
+ }
+ ]);
+ expect(getPublishedQuestions(TRIVIA_QUESTIONS)).toEqual([]);
+ expect(getPublishedQuestions(SIMPLE_QUESTIONS_2)).toEqual(
+ BACKUP_SIMPLE_QUESTIONS_2
+ );
+ expect(getPublishedQuestions(EMPTY_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Empty 1",
+ body: "This question is not empty, right?",
+ type: "multiple_choice_question",
+ options: ["correct", "it is", "not"],
+ expected: "correct",
+ points: 5,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Empty 2",
+ body: "",
+ type: "multiple_choice_question",
+ options: ["this", "one", "is", "not", "empty", "either"],
+ expected: "one",
+ points: 5,
+ published: true
+ },
+ {
+ id: 3,
+ name: "Empty 3",
+ body: "This questions is not empty either!",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 5,
+ published: true
+ },
+ {
+ id: 4,
+ name: "Empty 4",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "Even this one is not empty",
+ points: 5,
+ published: true
+ }
+ ]);
+ });
+
+ test("Testing the getNonEmptyQuestions functions", () => {
+ expect(getNonEmptyQuestions(BLANK_QUESTIONS)).toEqual([]);
+ expect(getNonEmptyQuestions(SIMPLE_QUESTIONS)).toEqual(
+ BACKUP_SIMPLE_QUESTIONS
+ );
+ expect(getNonEmptyQuestions(TRIVIA_QUESTIONS)).toEqual(
+ BACKUP_TRIVIA_QUESTIONS
+ );
+ expect(getNonEmptyQuestions(SIMPLE_QUESTIONS_2)).toEqual(
+ BACKUP_SIMPLE_QUESTIONS_2
+ );
+ expect(getNonEmptyQuestions(EMPTY_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Empty 1",
+ body: "This question is not empty, right?",
+ type: "multiple_choice_question",
+ options: ["correct", "it is", "not"],
+ expected: "correct",
+ points: 5,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Empty 2",
+ body: "",
+ type: "multiple_choice_question",
+ options: ["this", "one", "is", "not", "empty", "either"],
+ expected: "one",
+ points: 5,
+ published: true
+ },
+ {
+ id: 3,
+ name: "Empty 3",
+ body: "This questions is not empty either!",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 5,
+ published: true
+ },
+ {
+ id: 4,
+ name: "Empty 4",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "Even this one is not empty",
+ points: 5,
+ published: true
+ }
+ ]);
+ });
+
+ test("Testing the findQuestion function", () => {
+ expect(findQuestion(BLANK_QUESTIONS, 1)).toEqual(BLANK_QUESTIONS[0]);
+ expect(findQuestion(BLANK_QUESTIONS, 47)).toEqual(BLANK_QUESTIONS[1]);
+ expect(findQuestion(BLANK_QUESTIONS, 2)).toEqual(BLANK_QUESTIONS[2]);
+ expect(findQuestion(BLANK_QUESTIONS, 3)).toEqual(null);
+ expect(findQuestion(SIMPLE_QUESTIONS, 1)).toEqual(SIMPLE_QUESTIONS[0]);
+ expect(findQuestion(SIMPLE_QUESTIONS, 2)).toEqual(SIMPLE_QUESTIONS[1]);
+ expect(findQuestion(SIMPLE_QUESTIONS, 5)).toEqual(SIMPLE_QUESTIONS[2]);
+ expect(findQuestion(SIMPLE_QUESTIONS, 9)).toEqual(SIMPLE_QUESTIONS[3]);
+ expect(findQuestion(SIMPLE_QUESTIONS, 6)).toEqual(null);
+ expect(findQuestion(SIMPLE_QUESTIONS_2, 478)).toEqual(
+ SIMPLE_QUESTIONS_2[0]
+ );
+ expect(findQuestion([], 0)).toEqual(null);
+ });
+
+ test("Testing the removeQuestion", () => {
+ expect(removeQuestion(BLANK_QUESTIONS, 1)).toEqual([
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(removeQuestion(BLANK_QUESTIONS, 47)).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(removeQuestion(BLANK_QUESTIONS, 2)).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(removeQuestion(SIMPLE_QUESTIONS, 9)).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ },
+ {
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "firetruck"],
+ expected: "red",
+ points: 1,
+ published: true
+ }
+ ]);
+ expect(removeQuestion(SIMPLE_QUESTIONS, 5)).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ },
+ {
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: false
+ }
+ ]);
+ });
+
+ test("Testing the getNames function", () => {
+ expect(getNames(BLANK_QUESTIONS)).toEqual([
+ "Question 1",
+ "My New Question",
+ "Question 2"
+ ]);
+ expect(getNames(SIMPLE_QUESTIONS)).toEqual([
+ "Addition",
+ "Letters",
+ "Colors",
+ "Shapes"
+ ]);
+ expect(getNames(TRIVIA_QUESTIONS)).toEqual([
+ "Mascot",
+ "Motto",
+ "Goats"
+ ]);
+ expect(getNames(SIMPLE_QUESTIONS_2)).toEqual([
+ "Students",
+ "Importance",
+ "Sentience",
+ "Danger",
+ "Listening"
+ ]);
+ expect(getNames(EMPTY_QUESTIONS)).toEqual([
+ "Empty 1",
+ "Empty 2",
+ "Empty 3",
+ "Empty 4",
+ "Empty 5 (Actual)"
+ ]);
+ });
+
+ test("Testing the sumPoints function", () => {
+ expect(sumPoints(BLANK_QUESTIONS)).toEqual(3);
+ expect(sumPoints(SIMPLE_QUESTIONS)).toEqual(5);
+ expect(sumPoints(TRIVIA_QUESTIONS)).toEqual(20);
+ expect(sumPoints(EMPTY_QUESTIONS)).toEqual(25);
+ expect(sumPoints(SIMPLE_QUESTIONS_2)).toEqual(300);
+ });
+
+ test("Testing the sumPublishedPoints function", () => {
+ expect(sumPublishedPoints(BLANK_QUESTIONS)).toEqual(0);
+ expect(sumPublishedPoints(SIMPLE_QUESTIONS)).toEqual(2);
+ expect(sumPublishedPoints(TRIVIA_QUESTIONS)).toEqual(0);
+ expect(sumPublishedPoints(EMPTY_QUESTIONS)).toEqual(20);
+ expect(sumPublishedPoints(SIMPLE_QUESTIONS_2)).toEqual(300);
+ });
+
+ test("Testing the toCSV function", () => {
+ expect(toCSV(BLANK_QUESTIONS)).toEqual(`id,name,options,points,published
+1,Question 1,0,1,false
+47,My New Question,0,1,false
+2,Question 2,0,1,false`);
+ expect(toCSV(SIMPLE_QUESTIONS))
+ .toEqual(`id,name,options,points,published
+1,Addition,0,1,true
+2,Letters,0,1,false
+5,Colors,3,1,true
+9,Shapes,3,2,false`);
+ expect(toCSV(TRIVIA_QUESTIONS))
+ .toEqual(`id,name,options,points,published
+1,Mascot,3,7,false
+2,Motto,3,3,false
+3,Goats,3,10,false`);
+ expect(toCSV(EMPTY_QUESTIONS)).toEqual(`id,name,options,points,published
+1,Empty 1,3,5,true
+2,Empty 2,6,5,true
+3,Empty 3,0,5,true
+4,Empty 4,0,5,true
+5,Empty 5 (Actual),0,5,false`);
+ expect(toCSV(SIMPLE_QUESTIONS_2))
+ .toEqual(`id,name,options,points,published
+478,Students,0,53,true
+1937,Importance,0,47,true
+479,Sentience,0,40,true
+777,Danger,0,60,true
+1937,Listening,0,100,true`);
+ });
+
+ test("Testing the makeAnswers function", () => {
+ expect(makeAnswers(BLANK_QUESTIONS)).toEqual([
+ { questionId: 1, correct: false, text: "", submitted: false },
+ { questionId: 47, correct: false, text: "", submitted: false },
+ { questionId: 2, correct: false, text: "", submitted: false }
+ ]);
+ expect(makeAnswers(SIMPLE_QUESTIONS)).toEqual([
+ { questionId: 1, correct: false, text: "", submitted: false },
+ { questionId: 2, correct: false, text: "", submitted: false },
+ { questionId: 5, correct: false, text: "", submitted: false },
+ { questionId: 9, correct: false, text: "", submitted: false }
+ ]);
+ expect(makeAnswers(TRIVIA_QUESTIONS)).toEqual([
+ { questionId: 1, correct: false, text: "", submitted: false },
+ { questionId: 2, correct: false, text: "", submitted: false },
+ { questionId: 3, correct: false, text: "", submitted: false }
+ ]);
+ expect(makeAnswers(SIMPLE_QUESTIONS_2)).toEqual([
+ { questionId: 478, correct: false, text: "", submitted: false },
+ { questionId: 1937, correct: false, text: "", submitted: false },
+ { questionId: 479, correct: false, text: "", submitted: false },
+ { questionId: 777, correct: false, text: "", submitted: false },
+ { questionId: 1937, correct: false, text: "", submitted: false }
+ ]);
+ expect(makeAnswers(EMPTY_QUESTIONS)).toEqual([
+ { questionId: 1, correct: false, text: "", submitted: false },
+ { questionId: 2, correct: false, text: "", submitted: false },
+ { questionId: 3, correct: false, text: "", submitted: false },
+ { questionId: 4, correct: false, text: "", submitted: false },
+ { questionId: 5, correct: false, text: "", submitted: false }
+ ]);
+ });
+
+ test("Testing the publishAll function", () => {
+ expect(publishAll(BLANK_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: true
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: true
+ }
+ ]);
+ expect(publishAll(SIMPLE_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: true
+ },
+ {
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "firetruck"],
+ expected: "red",
+ points: 1,
+ published: true
+ },
+ {
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: true
+ }
+ ]);
+ expect(publishAll(TRIVIA_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Mascot",
+ body: "What is the name of the UD Mascot?",
+ type: "multiple_choice_question",
+ options: ["Bluey", "YoUDee", "Charles the Wonder Dog"],
+ expected: "YoUDee",
+ points: 7,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Motto",
+ body: "What is the University of Delaware's motto?",
+ type: "multiple_choice_question",
+ options: [
+ "Knowledge is the light of the mind",
+ "Just U Do it",
+ "Nothing, what's the motto with you?"
+ ],
+ expected: "Knowledge is the light of the mind",
+ points: 3,
+ published: true
+ },
+ {
+ id: 3,
+ name: "Goats",
+ body: "How many goats are there usually on the Green?",
+ type: "multiple_choice_question",
+ options: [
+ "Zero, why would there be goats on the green?",
+ "18420",
+ "Two"
+ ],
+ expected: "Two",
+ points: 10,
+ published: true
+ }
+ ]);
+ expect(publishAll(EMPTY_QUESTIONS)).toEqual([
+ {
+ id: 1,
+ name: "Empty 1",
+ body: "This question is not empty, right?",
+ type: "multiple_choice_question",
+ options: ["correct", "it is", "not"],
+ expected: "correct",
+ points: 5,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Empty 2",
+ body: "",
+ type: "multiple_choice_question",
+ options: ["this", "one", "is", "not", "empty", "either"],
+ expected: "one",
+ points: 5,
+ published: true
+ },
+ {
+ id: 3,
+ name: "Empty 3",
+ body: "This questions is not empty either!",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 5,
+ published: true
+ },
+ {
+ id: 4,
+ name: "Empty 4",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "Even this one is not empty",
+ points: 5,
+ published: true
+ },
+ {
+ id: 5,
+ name: "Empty 5 (Actual)",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 5,
+ published: true
+ }
+ ]);
+ expect(publishAll(SIMPLE_QUESTIONS_2)).toEqual(SIMPLE_QUESTIONS_2);
+ });
+
+ test("Testing the sameType function", () => {
+ expect(sameType([])).toEqual(true);
+ expect(sameType(BLANK_QUESTIONS)).toEqual(false);
+ expect(sameType(SIMPLE_QUESTIONS)).toEqual(false);
+ expect(sameType(TRIVIA_QUESTIONS)).toEqual(true);
+ expect(sameType(EMPTY_QUESTIONS)).toEqual(false);
+ expect(sameType(SIMPLE_QUESTIONS_2)).toEqual(true);
+ });
+
+ test("Testing the addNewQuestion function", () => {
+ expect(
+ addNewQuestion([], 142, "A new question", "short_answer_question")
+ ).toEqual([NEW_BLANK_QUESTION]);
+ expect(
+ addNewQuestion(
+ BLANK_QUESTIONS,
+ 142,
+ "A new question",
+ "short_answer_question"
+ )
+ ).toEqual([...BLANK_QUESTIONS, NEW_BLANK_QUESTION]);
+ expect(
+ addNewQuestion(
+ TRIVIA_QUESTIONS,
+ 449,
+ "Colors",
+ "multiple_choice_question"
+ )
+ ).toEqual([...TRIVIA_QUESTIONS, NEW_TRIVIA_QUESTION]);
+ });
+
+ test("Testing the renameQuestionById function", () => {
+ expect(renameQuestionById(BLANK_QUESTIONS, 1, "New Name")).toEqual([
+ {
+ id: 1,
+ name: "New Name",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(renameQuestionById(BLANK_QUESTIONS, 47, "Another Name")).toEqual(
+ [
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "Another Name",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]
+ );
+ expect(renameQuestionById(SIMPLE_QUESTIONS, 5, "Colours")).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ },
+ {
+ id: 5,
+ name: "Colours",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "firetruck"],
+ expected: "red",
+ points: 1,
+ published: true
+ },
+ {
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: false
+ }
+ ]);
+ });
+
+ test("Test the changeQuestionTypeById function", () => {
+ expect(
+ changeQuestionTypeById(
+ BLANK_QUESTIONS,
+ 1,
+ "multiple_choice_question"
+ )
+ ).toEqual(BLANK_QUESTIONS);
+ expect(
+ changeQuestionTypeById(BLANK_QUESTIONS, 1, "short_answer_question")
+ ).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(
+ changeQuestionTypeById(BLANK_QUESTIONS, 47, "short_answer_question")
+ ).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(
+ changeQuestionTypeById(TRIVIA_QUESTIONS, 3, "short_answer_question")
+ ).toEqual([
+ {
+ id: 1,
+ name: "Mascot",
+ body: "What is the name of the UD Mascot?",
+ type: "multiple_choice_question",
+ options: ["Bluey", "YoUDee", "Charles the Wonder Dog"],
+ expected: "YoUDee",
+ points: 7,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Motto",
+ body: "What is the University of Delaware's motto?",
+ type: "multiple_choice_question",
+ options: [
+ "Knowledge is the light of the mind",
+ "Just U Do it",
+ "Nothing, what's the motto with you?"
+ ],
+ expected: "Knowledge is the light of the mind",
+ points: 3,
+ published: false
+ },
+ {
+ id: 3,
+ name: "Goats",
+ body: "How many goats are there usually on the Green?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Two",
+ points: 10,
+ published: false
+ }
+ ]);
+ });
+
+ test("Testing the editOption function", () => {
+ expect(editOption(BLANK_QUESTIONS, 1, -1, "NEW OPTION")).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: ["NEW OPTION"],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(editOption(BLANK_QUESTIONS, 47, -1, "Another option")).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: ["Another option"],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(editOption(SIMPLE_QUESTIONS, 5, -1, "newspaper")).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ },
+ {
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "firetruck", "newspaper"],
+ expected: "red",
+ points: 1,
+ published: true
+ },
+ {
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: false
+ }
+ ]);
+ expect(editOption(SIMPLE_QUESTIONS, 5, 0, "newspaper")).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ },
+ {
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["newspaper", "apple", "firetruck"],
+ expected: "red",
+ points: 1,
+ published: true
+ },
+ {
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: false
+ }
+ ]);
+
+ expect(editOption(SIMPLE_QUESTIONS, 5, 2, "newspaper")).toEqual([
+ {
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ },
+ {
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ },
+ {
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "newspaper"],
+ expected: "red",
+ points: 1,
+ published: true
+ },
+ {
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: false
+ }
+ ]);
+ });
+
+ test("Testing the duplicateQuestionInArray function", () => {
+ expect(duplicateQuestionInArray(BLANK_QUESTIONS, 1, 27)).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 27,
+ name: "Copy of Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(duplicateQuestionInArray(BLANK_QUESTIONS, 47, 19)).toEqual([
+ {
+ id: 1,
+ name: "Question 1",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 47,
+ name: "My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 19,
+ name: "Copy of My New Question",
+ body: "",
+ type: "multiple_choice_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Question 2",
+ body: "",
+ type: "short_answer_question",
+ options: [],
+ expected: "",
+ points: 1,
+ published: false
+ }
+ ]);
+ expect(duplicateQuestionInArray(TRIVIA_QUESTIONS, 3, 111)).toEqual([
+ {
+ id: 1,
+ name: "Mascot",
+ body: "What is the name of the UD Mascot?",
+ type: "multiple_choice_question",
+ options: ["Bluey", "YoUDee", "Charles the Wonder Dog"],
+ expected: "YoUDee",
+ points: 7,
+ published: false
+ },
+ {
+ id: 2,
+ name: "Motto",
+ body: "What is the University of Delaware's motto?",
+ type: "multiple_choice_question",
+ options: [
+ "Knowledge is the light of the mind",
+ "Just U Do it",
+ "Nothing, what's the motto with you?"
+ ],
+ expected: "Knowledge is the light of the mind",
+ points: 3,
+ published: false
+ },
+ {
+ id: 3,
+ name: "Goats",
+ body: "How many goats are there usually on the Green?",
+ type: "multiple_choice_question",
+ options: [
+ "Zero, why would there be goats on the green?",
+ "18420",
+ "Two"
+ ],
+ expected: "Two",
+ points: 10,
+ published: false
+ },
+ {
+ id: 111,
+ name: "Copy of Goats",
+ body: "How many goats are there usually on the Green?",
+ type: "multiple_choice_question",
+ options: [
+ "Zero, why would there be goats on the green?",
+ "18420",
+ "Two"
+ ],
+ expected: "Two",
+ points: 10,
+ published: false
+ }
+ ]);
+ });
+
+ afterEach(() => {
+ expect(BLANK_QUESTIONS).toEqual(BACKUP_BLANK_QUESTIONS);
+ expect(SIMPLE_QUESTIONS).toEqual(BACKUP_SIMPLE_QUESTIONS);
+ expect(TRIVIA_QUESTIONS).toEqual(BACKUP_TRIVIA_QUESTIONS);
+ expect(SIMPLE_QUESTIONS_2).toEqual(BACKUP_SIMPLE_QUESTIONS_2);
+ expect(EMPTY_QUESTIONS).toEqual(BACKUP_EMPTY_QUESTIONS);
+ });
+});
diff --git a/src/nested.ts b/src/nested.ts
new file mode 100644
index 0000000000..23c4b0009e
--- /dev/null
+++ b/src/nested.ts
@@ -0,0 +1,263 @@
+import { Answer } from "./interfaces/answer";
+import { Question, QuestionType } from "./interfaces/question";
+import { makeBlankQuestion } from "./objects";
+import { duplicateQuestion } from "./objects";
+
+/**
+ * Consumes an array of questions and returns a new array with only the questions
+ * that are `published`.
+ */
+export function getPublishedQuestions(questions: Question[]): Question[] {
+ return questions.filter(question => question.published);
+ return [];
+}
+
+/**
+ * Consumes an array of questions and returns a new array of only the questions that are
+ * considered "non-empty". An empty question has an empty string for its `body` and
+ * `expected`, and an empty array for its `options`.
+ */
+export function getNonEmptyQuestions(questions: Question[]): Question[] {
+ return questions.filter(question =>
+ question.body.trim() !== "" ||
+ question.expected.trim() !== "" ||
+ question.options.length > 0
+ );
+ return [];
+}
+
+/***
+ * Consumes an array of questions and returns the question with the given `id`. If the
+ * question is not found, return `null` instead.
+ */
+export function findQuestion(
+ questions: Question[],
+ id: number
+): Question | null {
+ const found = questions.find(question => question.id === id);
+ return found || null;
+ return null;
+}
+
+/**
+ * Consumes an array of questions and returns a new array that does not contain the question
+ * with the given `id`.
+ */
+export function removeQuestion(questions: Question[], id: number): Question[] {
+ return questions.filter(question => question.id !== id);
+ return [];
+}
+
+/***
+ * Consumes an array of questions and returns a new array containing just the names of the
+ * questions, as an array.
+ */
+export function getNames(questions: Question[]): string[] {
+ return questions.map(question => question.name);
+ return [];
+}
+
+/***
+ * Consumes an array of questions and returns the sum total of all their points added together.
+ */
+export function sumPoints(questions: Question[]): number {
+ return questions.reduce((sum, question) => sum + question.points, 0);
+ return 0;
+}
+
+/***
+ * Consumes an array of questions and returns the sum total of the PUBLISHED questions.
+ */
+export function sumPublishedPoints(questions: Question[]): number {
+ return questions
+ .filter(question => question.published)
+ .reduce((sum, question) => sum + question.points, 0);
+ return 0;
+}
+
+/***
+ * Consumes an array of questions, and produces a Comma-Separated Value (CSV) string representation.
+ * A CSV is a type of file frequently used to share tabular data; we will use a single string
+ * to represent the entire file. The first line of the file is the headers "id", "name", "options",
+ * "points", and "published". The following line contains the value for each question, separated by
+ * commas. For the `options` field, use the NUMBER of options.
+ *
+ * Here is an example of what this will look like (do not include the border).
+ *`
+id,name,options,points,published
+1,Addition,0,1,true
+2,Letters,0,1,false
+5,Colors,3,1,true
+9,Shapes,3,2,false
+` *
+ * Check the unit tests for more examples!
+ */
+export function toCSV(questions: Question[]): string {
+ const headers = "id,name,options,points,published";
+ const rows = questions.map(question =>
+ `${question.id},${question.name},${question.options.length},${question.points},${question.published}`
+ );
+ return [headers, ...rows].join("\n");
+ return "";
+}
+
+/**
+ * Consumes an array of Questions and produces a corresponding array of
+ * Answers. Each Question gets its own Answer, copying over the `id` as the `questionId`,
+ * making the `text` an empty string, and using false for both `submitted` and `correct`.
+ */
+export function makeAnswers(questions: Question[]): Answer[] {
+ return questions.map(question => ({
+ questionId: question.id,
+ text: "",
+ submitted: false,
+ correct: false
+ }));
+ return [];
+}
+
+/***
+ * Consumes an array of Questions and produces a new array of questions, where
+ * each question is now published, regardless of its previous published status.
+ */
+export function publishAll(questions: Question[]): Question[] {
+ return questions.map(question => ({
+ ...question,
+ published: true
+ }));
+ return [];
+}
+
+/***
+ * Consumes an array of Questions and produces whether or not all the questions
+ * are the same type. They can be any type, as long as they are all the SAME type.
+ */
+export function sameType(questions: Question[]): boolean {
+ if (questions.length === 0) return true;
+ const firstType = questions[0].type;
+ return questions.every(question => question.type === firstType);
+ return false;
+}
+
+/***
+ * Consumes an array of Questions and produces a new array of the same Questions,
+ * except that a blank question has been added onto the end. Reuse the `makeBlankQuestion`
+ * you defined in the `objects.ts` file.
+ */
+export function addNewQuestion(
+ questions: Question[],
+ id: number,
+ name: string,
+ type: QuestionType
+): Question[] {
+ const newQuestion = makeBlankQuestion(id, name, type);
+ return [...questions, newQuestion];
+ return [];
+}
+
+/***
+ * Consumes an array of Questions and produces a new array of Questions, where all
+ * the Questions are the same EXCEPT for the one with the given `targetId`. That
+ * Question should be the same EXCEPT that its name should now be `newName`.
+ */
+export function renameQuestionById(
+ questions: Question[],
+ targetId: number,
+ newName: string
+): Question[] {
+ return questions.map(question =>
+ question.id === targetId
+ ? { ...question, name: newName }
+ : question
+ );
+ return [];
+}
+
+/***
+ * Consumes an array of Questions and produces a new array of Questions, where all
+ * the Questions are the same EXCEPT for the one with the given `targetId`. That
+ * Question should be the same EXCEPT that its `type` should now be the `newQuestionType`
+ * AND if the `newQuestionType` is no longer "multiple_choice_question" than the `options`
+ * must be set to an empty list.
+ */
+export function changeQuestionTypeById(
+ questions: Question[],
+ targetId: number,
+ newQuestionType: QuestionType
+): Question[] {
+ return questions.map(question => {
+ if (question.id === targetId) {
+ return {
+ ...question,
+ type: newQuestionType,
+ options: newQuestionType === "multiple_choice_question"
+ ? question.options
+ : []
+ };
+ }
+ return question;
+ });
+ return [];
+}
+
+/**
+ * Consumes an array of Questions and produces a new array of Questions, where all
+ * the Questions are the same EXCEPT for the one with the given `targetId`. That
+ * Question should be the same EXCEPT that its `option` array should have a new element.
+ * If the `targetOptionIndex` is -1, the `newOption` should be added to the end of the list.
+ * Otherwise, it should *replace* the existing element at the `targetOptionIndex`.
+ *
+ * Remember, if a function starts getting too complicated, think about how a helper function
+ * can make it simpler! Break down complicated tasks into little pieces.
+ */
+export function editOption(
+ questions: Question[],
+ targetId: number,
+ targetOptionIndex: number,
+ newOption: string
+): Question[] {
+ return questions.map(question => {
+ if (question.id !== targetId) {
+ return question;
+ }
+
+ const newOptions = [...question.options];
+
+ if (targetOptionIndex === -1) {
+ newOptions.push(newOption);
+ } else {
+ newOptions[targetOptionIndex] = newOption;
+ }
+
+ return {
+ ...question,
+ options: newOptions
+ };
+ });
+ return [];
+}
+
+/***
+ * Consumes an array of questions, and produces a new array based on the original array.
+ * The only difference is that the question with id `targetId` should now be duplicated, with
+ * the duplicate inserted directly after the original question. Use the `duplicateQuestion`
+ * function you defined previously; the `newId` is the parameter to use for the duplicate's ID.
+ */
+export function duplicateQuestionInArray(
+ questions: Question[],
+ targetId: number,
+ newId: number
+): Question[] {
+ const result: Question[] = [];
+
+ for (const question of questions) {
+ result.push(question);
+ if (question.id === targetId) {
+ const duplicated = duplicateQuestion(newId, question);
+ result.push(duplicated);
+ }
+ }
+
+ return result;
+ return [];
+}
diff --git a/src/objects.test.ts b/src/objects.test.ts
new file mode 100644
index 0000000000..a9c76a334e
--- /dev/null
+++ b/src/objects.test.ts
@@ -0,0 +1,295 @@
+import { Question } from "./interfaces/question";
+import {
+ makeBlankQuestion,
+ isCorrect,
+ isValid,
+ toShortForm,
+ toMarkdown,
+ duplicateQuestion,
+ renameQuestion,
+ publishQuestion,
+ addOption,
+ mergeQuestion
+} from "./objects";
+import testQuestionData from "./data/questions.json";
+import backupQuestionData from "./data/questions.json";
+
+////////////////////////////////////////////
+// Setting up the test data
+
+const { BLANK_QUESTIONS, SIMPLE_QUESTIONS }: Record =
+ // Typecast the test data that we imported to be a record matching
+ // strings to the question list
+ testQuestionData as Record;
+
+// We have backup versions of the data to make sure all changes are immutable
+const {
+ BLANK_QUESTIONS: BACKUP_BLANK_QUESTIONS,
+ SIMPLE_QUESTIONS: BACKUP_SIMPLE_QUESTIONS
+}: Record = backupQuestionData as Record<
+ string,
+ Question[]
+>;
+
+// Unpack the list of simple questions into convenient constants
+const [ADDITION_QUESTION, LETTER_QUESTION, COLOR_QUESTION, SHAPE_QUESTION] =
+ SIMPLE_QUESTIONS;
+const [
+ BACKUP_ADDITION_QUESTION,
+ BACKUP_LETTER_QUESTION,
+ BACKUP_COLOR_QUESTION,
+ BACKUP_SHAPE_QUESTION
+] = BACKUP_SIMPLE_QUESTIONS;
+
+////////////////////////////////////////////
+// Actual tests
+
+describe("Testing the object functions", () => {
+ //////////////////////////////////
+ // makeBlankQuestion
+
+ test("Testing the makeBlankQuestion function", () => {
+ expect(
+ makeBlankQuestion(1, "Question 1", "multiple_choice_question")
+ ).toEqual(BLANK_QUESTIONS[0]);
+ expect(
+ makeBlankQuestion(47, "My New Question", "multiple_choice_question")
+ ).toEqual(BLANK_QUESTIONS[1]);
+ expect(
+ makeBlankQuestion(2, "Question 2", "short_answer_question")
+ ).toEqual(BLANK_QUESTIONS[2]);
+ });
+
+ ///////////////////////////////////
+ // isCorrect
+ test("Testing the isCorrect function", () => {
+ expect(isCorrect(ADDITION_QUESTION, "4")).toEqual(true);
+ expect(isCorrect(ADDITION_QUESTION, "2")).toEqual(false);
+ expect(isCorrect(ADDITION_QUESTION, " 4\n")).toEqual(true);
+ expect(isCorrect(LETTER_QUESTION, "Z")).toEqual(true);
+ expect(isCorrect(LETTER_QUESTION, "z")).toEqual(true);
+ expect(isCorrect(LETTER_QUESTION, "4")).toEqual(false);
+ expect(isCorrect(LETTER_QUESTION, "0")).toEqual(false);
+ expect(isCorrect(LETTER_QUESTION, "zed")).toEqual(false);
+ expect(isCorrect(COLOR_QUESTION, "red")).toEqual(true);
+ expect(isCorrect(COLOR_QUESTION, "apple")).toEqual(false);
+ expect(isCorrect(COLOR_QUESTION, "firetruck")).toEqual(false);
+ expect(isCorrect(SHAPE_QUESTION, "square")).toEqual(false);
+ expect(isCorrect(SHAPE_QUESTION, "triangle")).toEqual(false);
+ expect(isCorrect(SHAPE_QUESTION, "circle")).toEqual(true);
+ });
+
+ ///////////////////////////////////
+ // isValid
+ test("Testing the isValid function", () => {
+ expect(isValid(ADDITION_QUESTION, "4")).toEqual(true);
+ expect(isValid(ADDITION_QUESTION, "2")).toEqual(true);
+ expect(isValid(ADDITION_QUESTION, " 4\n")).toEqual(true);
+ expect(isValid(LETTER_QUESTION, "Z")).toEqual(true);
+ expect(isValid(LETTER_QUESTION, "z")).toEqual(true);
+ expect(isValid(LETTER_QUESTION, "4")).toEqual(true);
+ expect(isValid(LETTER_QUESTION, "0")).toEqual(true);
+ expect(isValid(LETTER_QUESTION, "zed")).toEqual(true);
+ expect(isValid(COLOR_QUESTION, "red")).toEqual(true);
+ expect(isValid(COLOR_QUESTION, "apple")).toEqual(true);
+ expect(isValid(COLOR_QUESTION, "firetruck")).toEqual(true);
+ expect(isValid(COLOR_QUESTION, "RED")).toEqual(false);
+ expect(isValid(COLOR_QUESTION, "orange")).toEqual(false);
+ expect(isValid(SHAPE_QUESTION, "square")).toEqual(true);
+ expect(isValid(SHAPE_QUESTION, "triangle")).toEqual(true);
+ expect(isValid(SHAPE_QUESTION, "circle")).toEqual(true);
+ expect(isValid(SHAPE_QUESTION, "circle ")).toEqual(false);
+ expect(isValid(SHAPE_QUESTION, "rhombus")).toEqual(false);
+ });
+
+ ///////////////////////////////////
+ // toShortForm
+ test("Testing the toShortForm function", () => {
+ expect(toShortForm(ADDITION_QUESTION)).toEqual("1: Addition");
+ expect(toShortForm(LETTER_QUESTION)).toEqual("2: Letters");
+ expect(toShortForm(COLOR_QUESTION)).toEqual("5: Colors");
+ expect(toShortForm(SHAPE_QUESTION)).toEqual("9: Shapes");
+ expect(toShortForm(BLANK_QUESTIONS[1])).toEqual("47: My New Que");
+ });
+
+ ///////////////////////////////////
+ // toMarkdown
+ test("Testing the toMarkdown function", () => {
+ expect(toMarkdown(ADDITION_QUESTION)).toEqual(`# Addition
+What is 2+2?`);
+ expect(toMarkdown(LETTER_QUESTION)).toEqual(`# Letters
+What is the last letter of the English alphabet?`);
+ expect(toMarkdown(COLOR_QUESTION)).toEqual(`# Colors
+Which of these is a color?
+- red
+- apple
+- firetruck`);
+ expect(toMarkdown(SHAPE_QUESTION)).toEqual(`# Shapes
+What shape can you make with one line?
+- square
+- triangle
+- circle`);
+ });
+
+ afterEach(() => {
+ expect(ADDITION_QUESTION).toEqual(BACKUP_ADDITION_QUESTION);
+ expect(LETTER_QUESTION).toEqual(BACKUP_LETTER_QUESTION);
+ expect(SHAPE_QUESTION).toEqual(BACKUP_SHAPE_QUESTION);
+ expect(COLOR_QUESTION).toEqual(BACKUP_COLOR_QUESTION);
+ expect(BLANK_QUESTIONS).toEqual(BACKUP_BLANK_QUESTIONS);
+ });
+
+ ///////////////////////////////////
+ // renameQuestion
+ test("Testing the renameQuestion function", () => {
+ expect(
+ renameQuestion(ADDITION_QUESTION, "My Addition Question")
+ ).toEqual({
+ id: 1,
+ name: "My Addition Question",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ });
+ expect(
+ renameQuestion(SHAPE_QUESTION, "I COMPLETELY CHANGED THIS NAME")
+ ).toEqual({
+ id: 9,
+ name: "I COMPLETELY CHANGED THIS NAME",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 2,
+ published: false
+ });
+ });
+
+ ///////////////////////////////////
+ // publishQuestion
+ test("Testing the publishQuestion function", () => {
+ expect(publishQuestion(ADDITION_QUESTION)).toEqual({
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: false
+ });
+ expect(publishQuestion(LETTER_QUESTION)).toEqual({
+ id: 2,
+ name: "Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: true
+ });
+ expect(publishQuestion(publishQuestion(ADDITION_QUESTION))).toEqual({
+ id: 1,
+ name: "Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: true
+ });
+ });
+
+ ///////////////////////////////////
+ // duplicateQuestion
+ test("Testing the duplicateQuestion function", () => {
+ expect(duplicateQuestion(9, ADDITION_QUESTION)).toEqual({
+ id: 9,
+ name: "Copy of Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 1,
+ published: false
+ });
+ expect(duplicateQuestion(55, LETTER_QUESTION)).toEqual({
+ id: 55,
+ name: "Copy of Letters",
+ body: "What is the last letter of the English alphabet?",
+ type: "short_answer_question",
+ options: [],
+ expected: "Z",
+ points: 1,
+ published: false
+ });
+ });
+
+ ///////////////////////////////////
+ // addOption
+ test("Testing the addOption function", () => {
+ expect(addOption(SHAPE_QUESTION, "heptagon")).toEqual({
+ id: 9,
+ name: "Shapes",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle", "heptagon"],
+ expected: "circle",
+ points: 2,
+ published: false
+ });
+ expect(addOption(COLOR_QUESTION, "squiggles")).toEqual({
+ id: 5,
+ name: "Colors",
+ body: "Which of these is a color?",
+ type: "multiple_choice_question",
+ options: ["red", "apple", "firetruck", "squiggles"],
+ expected: "red",
+ points: 1,
+ published: true
+ });
+ });
+
+ ///////////////////////////////////
+ // mergeQuestion
+ test("Testing the mergeQuestion function", () => {
+ expect(
+ mergeQuestion(
+ 192,
+ "More Points Addition",
+ ADDITION_QUESTION,
+ SHAPE_QUESTION
+ )
+ ).toEqual({
+ id: 192,
+ name: "More Points Addition",
+ body: "What is 2+2?",
+ type: "short_answer_question",
+ options: [],
+ expected: "4",
+ points: 2,
+ published: false
+ });
+
+ expect(
+ mergeQuestion(
+ 99,
+ "Less Points Shape",
+ SHAPE_QUESTION,
+ ADDITION_QUESTION
+ )
+ ).toEqual({
+ id: 99,
+ name: "Less Points Shape",
+ body: "What shape can you make with one line?",
+ type: "multiple_choice_question",
+ options: ["square", "triangle", "circle"],
+ expected: "circle",
+ points: 1,
+ published: false
+ });
+ });
+});
diff --git a/src/objects.ts b/src/objects.ts
new file mode 100644
index 0000000000..a86bbf61ab
--- /dev/null
+++ b/src/objects.ts
@@ -0,0 +1,188 @@
+import { Question, QuestionType } from "./interfaces/question";
+
+/**
+ * Create a new blank question with the given `id`, `name`, and `type. The `body` and
+ * `expected` should be empty strings, the `options` should be an empty list, the `points`
+ * should default to 1, and `published` should default to false.
+ */
+export function makeBlankQuestion(
+ id: number,
+ name: string,
+ type: QuestionType
+): Question {
+ const newQuestion: Question = {
+ id: id,
+ name: name,
+ type: type,
+ body: "",
+ expected: "",
+ options: [],
+ points: 1,
+ published: false
+ };
+ return newQuestion;
+ return {};
+}
+
+/**
+ * Consumes a question and a potential `answer`, and returns whether or not
+ * the `answer` is correct. You should check that the `answer` is equal to
+ * the `expected`, ignoring capitalization and trimming any whitespace.
+ *
+ * HINT: Look up the `trim` and `toLowerCase` functions.
+ */
+export function isCorrect(question: Question, answer: string): boolean {
+ const normalizedAnswer: string = answer.trim().toLowerCase();
+ const normalizedExpected: string = question.expected.trim().toLowerCase();
+ const isAnswerCorrect: boolean = normalizedAnswer === normalizedExpected;
+ return isAnswerCorrect;
+ return false;
+}
+
+/**
+ * Consumes a question and a potential `answer`, and returns whether or not
+ * the `answer` is valid (but not necessarily correct). For a `short_answer_question`,
+ * any answer is valid. But for a `multiple_choice_question`, the `answer` must
+ * be exactly one of the options.
+ */
+export function isValid(question: Question, answer: string): boolean {
+ if (question.type === "short_answer_question") {
+ return true;
+ }
+ const isValidAnswer: boolean = question.options.includes(answer);
+ return isValidAnswer;
+ return false;
+}
+
+/**
+ * Consumes a question and produces a string representation combining the
+ * `id` and first 10 characters of the `name`. The two strings should be
+ * separated by ": ". So for example, the question with id 9 and the
+ * name "My First Question" would become "9: My First Q".
+ */
+export function toShortForm(question: Question): string {
+ const shortName: string = question.name.slice(0, 10);
+ const shortFormResult: string = `${question.id}: ${shortName}`;
+ return shortFormResult;
+ return "";
+}
+
+/**
+ * Consumes a question and returns a formatted string representation as follows:
+ * - The first line should be a hash sign, a space, and then the `name`
+ * - The second line should be the `body`
+ * - If the question is a `multiple_choice_question`, then the following lines
+ * need to show each option on its line, preceded by a dash and space.
+ *
+ * The example below might help, but don't include the border!
+ * ----------Example-------------
+ * |# Name |
+ * |The body goes here! |
+ * |- Option 1 |
+ * |- Option 2 |
+ * |- Option 3 |
+ * ------------------------------
+ * Check the unit tests for more examples of what this looks like!
+ */
+export function toMarkdown(question: Question): string {
+ const lines: string[] = [`# ${question.name}`, question.body];
+
+ if (question.type === "multiple_choice_question") {
+ for (const option of question.options) {
+ lines.push(`- ${option}`);
+ }
+ }
+
+ const markdownResult: string = lines.join("\n");
+ return markdownResult;
+ return "";
+}
+
+/**
+ * Return a new version of the given question, except the name should now be
+ * `newName`.
+ */
+export function renameQuestion(question: Question, newName: string): Question {
+ const renamed: Question = {
+ ...question,
+ name: newName
+ };
+ return renamed;
+ return question;
+}
+
+/**
+ * Return a new version of the given question, except the `published` field
+ * should be inverted. If the question was not published, now it should be
+ * published; if it was published, now it should be not published.
+ */
+export function publishQuestion(question: Question): Question {
+ const published: Question = {
+ ...question,
+ published: !question.published
+ };
+ return published;
+ return question;
+}
+
+/**
+ * Create a new question based on the old question, copying over its `body`, `type`,
+ * `options`, `expected`, and `points` without changes. The `name` should be copied
+ * over as "Copy of ORIGINAL NAME" (e.g., so "Question 1" would become "Copy of Question 1").
+ * The `published` field should be reset to false.
+ */
+export function duplicateQuestion(id: number, oldQuestion: Question): Question {
+ const duplicated: Question = {
+ ...oldQuestion,
+ id: id,
+ name: `Copy of ${oldQuestion.name}`,
+ published: false
+ };
+ return duplicated;
+ return oldQuestion;
+}
+
+/**
+ * Return a new version of the given question, with the `newOption` added to
+ * the list of existing `options`. Remember that the new Question MUST have
+ * its own separate copy of the `options` list, rather than the same reference
+ * to the original question's list!
+ * Check out the subsection about "Nested Fields" for more information.
+ */
+export function addOption(question: Question, newOption: string): Question {
+ const newOptions: string[] = [...question.options, newOption];
+ const withNewOption: Question = {
+ ...question,
+ options: newOptions
+ };
+ return withNewOption;
+ return question;
+}
+
+/**
+ * Consumes an id, name, and two questions, and produces a new question.
+ * The new question will use the `body`, `type`, `options`, and `expected` of the
+ * `contentQuestion`. The second question will provide the `points`.
+ * The `published` status should be set to false.
+ * Notice that the second Question is provided as just an object with a `points`
+ * field; but the function call would be the same as if it were a `Question` type!
+ */
+export function mergeQuestion(
+ id: number,
+ name: string,
+ contentQuestion: Question,
+ { points }: { points: number }
+): Question {
+ const merged: Question = {
+ id: id,
+ name: name,
+ body: contentQuestion.body,
+ type: contentQuestion.type,
+ options: [...contentQuestion.options],
+ expected: contentQuestion.expected,
+ points: points,
+ published: false
+ };
+ return merged;
+ return contentQuestion;
+}
diff --git a/src/package.json b/src/package.json
new file mode 100644
index 0000000000..fc2b66a549
--- /dev/null
+++ b/src/package.json
@@ -0,0 +1,78 @@
+{
+ "name": "react-typescript-starter",
+ "homepage": "homepage",
+ "version": "0.1.0",
+ "private": true,
+ "dependencies": {
+ "@testing-library/jest-dom": "^5.17.0",
+ "@testing-library/user-event": "^13.5.0",
+ "@types/jest": "^27.5.2",
+ "@types/node": "^16.18.105",
+ "react": "^18.3.1",
+ "react-bootstrap": "^2.10.4",
+ "react-dom": "^18.3.1",
+ "react-scripts": "5.0.1",
+ "typescript": "^4.9.5",
+ "web-vitals": "^2.1.4"
+ },
+ "scripts": {
+ "start": "react-scripts start",
+ "build": "react-scripts build",
+ "test": "react-scripts test",
+ "test:cov": "react-scripts test --coverage --watchAll",
+ "test:json": "react-scripts test --json --watchAll=false --outputFile jest-output.json --coverage",
+ "eject": "react-scripts eject",
+ "lint": "eslint ./src --ext .tsx --ext .ts --max-warnings 0",
+ "eslint-output": "eslint-output ./src --ext .tsx --ext .ts --max-warnings 0",
+ "format": "prettier --config .prettierrc --write src/**/*.{ts,tsx}"
+ },
+ "eslintConfig": {
+ "extends": [
+ "react-app",
+ "react-app/jest"
+ ]
+ },
+ "browserslist": {
+ "production": [
+ ">0.2%",
+ "not dead",
+ "not op_mini all"
+ ],
+ "development": [
+ "last 1 chrome version",
+ "last 1 firefox version",
+ "last 1 safari version"
+ ]
+ },
+ "devDependencies": {
+ "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
+ "@babel/plugin-transform-private-property-in-object": "^7.24.7",
+ "@testing-library/dom": "^10.4.0",
+ "@testing-library/react": "^16.0.0",
+ "@types/react": "^18.3.3",
+ "@types/react-dom": "^18.3.0",
+ "@typescript-eslint/eslint-plugin": "^8.2.0",
+ "@typescript-eslint/parser": "^8.2.0",
+ "eslint": "^8.57.0",
+ "eslint-config-prettier": "^9.1.0",
+ "eslint-output": "^3.0.1",
+ "eslint-plugin-prettier": "^5.2.1",
+ "eslint-plugin-react": "^7.35.0",
+ "gh-pages": "^6.1.1",
+ "jest": "^29.7.0",
+ "jest-environment-jsdom": "^29.7.0",
+ "prettier": "^3.3.3",
+ "prettier-eslint": "^16.3.0",
+ "ts-jest": "^29.2.4",
+ "ts-node": "^10.9.2"
+ },
+ "jest": {
+ "collectCoverageFrom": [
+ "src/**/*.ts",
+ "src/**/*.tsx",
+ "!src/index.tsx",
+ "!src/reportWebVitals.ts",
+ "!src/react-app-env.d.ts"
+ ]
+ }
+}
diff --git a/src/questions.json b/src/questions.json
new file mode 100644
index 0000000000..9be5ea6f22
--- /dev/null
+++ b/src/questions.json
@@ -0,0 +1,23 @@
+[
+ {
+ "id": 1,
+ "name": "Sample Question 1",
+ "type": "short_answer_question",
+ "body": "What is 2 + 2?",
+ "expected": "4",
+ "options": [],
+ "points": 1,
+ "published": false
+ },
+ {
+ "id": 2,
+ "name": "Sample Question 2",
+ "type": "multiple_choice_question",
+ "body": "Select the correct option.",
+ "expected": "B",
+ "options": ["A", "B", "C"],
+ "points": 2,
+ "published": true
+ }
+]
+
diff --git a/src/questions.json.ts b/src/questions.json.ts
new file mode 100644
index 0000000000..30378e6666
--- /dev/null
+++ b/src/questions.json.ts
@@ -0,0 +1,17 @@
+/*
+import questions from "./questions.json";
+import { Question } from "./interfaces/question";
+
+const typedQuestions: Question[] = questions.map((q: Question) => ({
+ id: q.id,
+ name: q.name,
+ type: q.type,
+ body: q.body,
+ expected: q.expected,
+ options: [...q.options],
+ points: q.points,
+ published: q.published,
+}));
+
+export default typedQuestions;
+*/
diff --git a/src/questions.ts b/src/questions.ts
new file mode 100644
index 0000000000..a693e7ed4e
--- /dev/null
+++ b/src/questions.ts
@@ -0,0 +1,5 @@
+import rawQuestions from "./questions.json";
+import { Question } from "./interfaces/question";
+
+// Cast JSON data into Question[]
+export const SIMPLE_QUESTIONS: Question[] = rawQuestions as Question[];
diff --git a/src/tsconfig.json b/src/tsconfig.json
new file mode 100644
index 0000000000..9be842205f
--- /dev/null
+++ b/src/tsconfig.json
@@ -0,0 +1,21 @@
+{
+ "compilerOptions": {
+ "target": "es5",
+ "lib": ["dom", "dom.iterable", "esnext"],
+ "allowJs": true,
+ "skipLibCheck": true,
+ "esModuleInterop": true,
+ "allowSyntheticDefaultImports": true,
+ "strict": true,
+ "forceConsistentCasingInFileNames": true,
+ "noFallthroughCasesInSwitch": true,
+ "module": "esnext",
+ "moduleResolution": "node",
+ "resolveJsonModule": true,
+ "isolatedModules": true,
+ "noEmit": true,
+ "jsx": "react-jsx",
+ "noImplicitAny": true
+ },
+ "include": ["src"]
+}
diff --git a/src/typedQuestions.ts b/src/typedQuestions.ts
new file mode 100644
index 0000000000..d22b6511f6
--- /dev/null
+++ b/src/typedQuestions.ts
@@ -0,0 +1,15 @@
+/*
+import questionsData from "./questions.json";
+import { Question } from "./interfaces/question";
+
+export const questions: Question[] = questionsData.map((q: Question) => ({
+ id: q.id,
+ name: q.name,
+ type: q.type,
+ body: q.body,
+ expected: q.expected,
+ options: [...q.options],
+ points: q.points,
+ published: q.published,
+}));
+*/