Skip to content

Add object manipulation functionalitySolved objects #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

// 1. Creating Objects
let drBart = { title: "Dr. Bart", shirtColor: "blue", isProfessor: true };
let ada = { height: 23, name: "Ada Bart" };
let cisc275 = {
id: "CISC275",
seats: 80,
online: false,
labs: ["20", "21", "22"],
};
let emptyObject = {};

// 2. Accessing Object Properties
console.log(drBart.shirtColor); // Output: "blue"
console.log(ada.height); // Output: 23
console.log(cisc275.labs); // Output: ['20', '21', '22']

// 3. Updating Objects Immutably
const myPhone = { brand: "Samsung", model: "Galaxy", batteryLeft: 97 };
const usedPhone = { ...myPhone, batteryLeft: 48 };
console.log(usedPhone.batteryLeft); // Output: 48

// 4. Nested Data (Updating Nested Arrays)
const student = { name: "Ada", grades: [100, 99, 78, 97] };
const studentWithNewGrade = {
...student,
grades: [...student.grades, 100], // Add a new grade to the array
};
console.log(studentWithNewGrade.grades); // Output: [100, 99, 78, 97, 100]

// 5. Interfaces (Defining Object Structure)
interface Dog {
name: string;
age: number;
breed: "Corgi" | "Chihuahua" | "Mutt";
fuzzy: boolean;
}

// 6. Creating Objects Using Interfaces
const adaDog: Dog = { name: "Ada Bart", breed: "Corgi", age: 4, fuzzy: true };
console.log(adaDog.name); // Output: "Ada Bart"

// 7. Records (Key-Value Pairs)
const courseLookup: Record<string, string> = {
CISC108: "Introduction to Computer Science",
CISC210: "Systems Programming",
CISC220: "Data Structures",
CISC275: "Introduction to Software Engineering",
};
console.log(courseLookup["CISC275"]); // Output: "Introduction to Software Engineering"

// 8. Destructuring (Unpacking Object Properties)
const { name, breed } = adaDog;
console.log(name); // Output: "Ada Bart"
console.log(breed); // Output: "Corgi"

// 9. JSON (Converting Objects to Strings and Back)
const someData = { name: "Blah Blah", isCool: true, nums: [1, 7, 9] };
const dataAsText = JSON.stringify(someData);
console.log(dataAsText); // Output: "{"name":"Blah Blah","isCool":true,"nums":[1,7,9]}"
const dataAgain = JSON.parse(dataAsText);
console.log(dataAgain.name); // Output: "Blah Blah"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"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",
Expand Down
5 changes: 5 additions & 0 deletions public/tasks/task-objects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Task - Objects

Version: 0.0.1

Implement functions that work with objects immutably.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function App(): React.JSX.Element {
return (
<div className="App">
<header className="App-header">
UM COS420 with React Hooks and TypeScript
Hello, my name is Denis Sima!
</header>
<p>
Edit <code>src/App.tsx</code> and save. This page will
Expand Down
79 changes: 79 additions & 0 deletions src/data/questions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"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
}
],
"SIMPLE_QUESTIONS_2": [],
"EMPTY_QUESTIONS": [],
"TRIVIA_QUESTIONS": []
}
21 changes: 21 additions & 0 deletions src/interfaces/question.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** QuestionType influences how a question is asked and what kinds of answers are possible */
export type QuestionType = "multiple_choice_question" | "short_answer_question";

export interface Question {
/** A unique identifier for the question */
id: number;
/** The human-friendly title of the question */
name: string;
/** The instructions and content of the Question */
body: string;
/** The kind of Question; influences how the user answers and what options are displayed */
type: QuestionType;
/** The possible answers for a Question (for Multiple Choice questions) */
options: string[];
/** The actually correct answer expected */
expected: string;
/** How many points this question is worth, roughly indicating its importance and difficulty */
points: number;
/** Whether or not this question is ready to display to students */
published: boolean;
}
Loading