Skip to content

Commit 74b79ea

Browse files
committed
Added week 4
1 parent 86bdc52 commit 74b79ea

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

week-4/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
You have been given the code of a purely frontend TODO app
2+
You have to fill in the following functions -
3+
- addTodoToDom
4+
- removeTodoFromDom
5+
- updateTodoInDom
6+
- updateState
7+
8+
These 4 functions comprise of what it means to create a library like React.
9+
The goal is the following -
10+
1. Any time the updateState function is called with a new state, the updateState function calculates the diff between newTodos and oldTodos and call `addTodoToDom`, `removeTodoFromDom` or `updateState` based on the calculated diff.
11+
2. They id of a todo uniquely identifies it. If the title of a todo with the same id changes in two iterations, updateTodoInDom should be called for it.
12+
3. The structure of the state variable looks something like this -
13+
```js
14+
const todos = [{
15+
title: "Go to gym",
16+
description: "Go to gym from 7-8PM",
17+
id: 1
18+
}]
19+
```

week-4/index.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<script>
6+
let globalId = 1;
7+
let todoState = [];
8+
let oldTodoState = [];
9+
10+
function addTodoToDom() {
11+
12+
}
13+
14+
function removeTodoFromDom(todo) {
15+
16+
}
17+
18+
function updateTodoInDom(oldTodo, newTodo) {
19+
20+
}
21+
22+
function updateState(newTodos) {
23+
// calculate the diff b/w newTodos and oldTodos.
24+
// More specifically, find out what todos are -
25+
// 1. added
26+
// 2. deleted
27+
// 3. updated
28+
const added = [];
29+
const deleted = [];
30+
const updated = [];
31+
// calculate these 3 arrays
32+
// call addTodo, removeTodo, updateTodo functions on each of the
33+
// elements
34+
oldTodoState = newTodos;
35+
}
36+
37+
function addTodo() {
38+
const title = document.getElementById("title").value;
39+
const description = document.getElementById("description").value;
40+
todoState.push({
41+
title: title,
42+
description: description,
43+
id: globalId++,
44+
})
45+
updateState(todoState);
46+
}
47+
</script>
48+
</head>
49+
50+
<body>
51+
<input type="text" id="title" placeholder="Todo title"></input> <br /><br />
52+
<input type="text" id="description" placeholder="Todo description"></input> <br /><br />
53+
<button onclick="addTodo()">Add todo</button>
54+
<br /> <br />
55+
56+
<div id="todos">
57+
58+
</div>
59+
</body>
60+
61+
</html>

0 commit comments

Comments
 (0)