-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescription-howToCode
More file actions
742 lines (605 loc) · 21.2 KB
/
description-howToCode
File metadata and controls
742 lines (605 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// VERSION 1 - Arrays
// 1 It should have a place to store todos
// 2 It should have a way to display todos
// 3 It should have a way to add new todos
// 4 It should have a way to change a todo
// 5 It should have a way to delete a todo
/*-------------------------------------*/
// 1 - It should have a place to store todos
// We just make a list of items, like a list
// ["item 1", "item 2", "item 3"]
// We then want to give a name to the list, that is a variable (var)
// ACTION var todos = ["item 1", "item 2", "item 3"]
// It should have a place to store todos → (DONE)
/*-------------------------------------*/
// 2 - It should have a way to display todos
// We print with the console.log() function
// ACTION console.log("My todos:", todos)
// we don't need quotations because we want the array to be printed, not the string
// It should have a way to display todos → (DONE)
/*-------------------------------------*/
// 3 - It should have a way to add new todos
// With the "push" command, we add an item at the end of an array
// ACTION todos.push("item 4")
// It should have a way to add new todos → (DONE)
/*-------------------------------------*/
// 4 - It should have a way to change a todo
// To change an array, we first look inside the array, to know the position of the item we wanna change
// Then we put the position in the square brackets
// To see the first item: todos[0]
// ACTION todos[0] = "changed item"
// It should have a way to change a todo → (DONE)
/*-------------------------------------*/
// 5 - It should have a way to delete todos
// We delete items with the "splice" command;
// we need 2 things: the position, the amount of items to be deleted
// ACTION todos.splice(0, 1) | It'll delete the first item
// It should have a way to delete todos → (DONE)
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 2 - Functions
// 6 - Functions are just recipes
/*
makeTurkeySandwich
get one slice of bread
add turkey
put a slice of bread on top
*/
/*
Structure of a javascript function
function makeTurkeySandwich() {
get one slice of bread;
add turkey;
put a slice of bread on top;
}
*/
// To run the function: makeTurkeySandwich()
/*-------------------------------------*/
// 7 - Customizing functions
/*
makeSandwichWith _____
get one slice of bread
add _____
put a slice of bread on top
*/
// Structure of a javascript function
/*
function makeSandwichWith(filling) {
get one slice of bread;
add filling;
put a slice of bread on top;
}
*/
// makeSandwichWith("ham")
/*-------------------------------------*/
// 8 - Requirements
/*
- It should have a function to display todos
- It should have a function to add todos
- It should have a function to change todos
- It should have a function to delete todos
*/
/*-------------------------------------*/
// 9 - It should have a function to display todos
var todos = ["item 1", "item 2", "item 3"]
function displayTodos() {
console.log("My todos", todos);
}
/*-------------------------------------*/
// 10 - It should have a function to add todos
// The way you add items is through the "push" command
function addTodo(todo) {
todos.push(todo);
displayTodos(); // this way we can see what's in the list
}
/*-------------------------------------*/
// 11 - It should have a function to change todos
function changeTodo(position, newValue) {
todos[position] = newValue;
displayTodos();
}
// Running example: changeTodo(0, "newItem")
// 12 - It should have a function to delete todos
function deleteTodo(position) {
todos.splice(position, 1);
// first item gives the position of the item, the second gives the quantity to be deleted
displayTodos();
}
// to run the function: deleteTodo(2)
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 3 - Objects
// 13 - What is an object
// An object is group of data surrounded by curly braces
// We assign it a variable
var myComputer = {
// all these are properties: name: value,
operatingSystem: "linux",
screenSize: "15 inches",
purchaseYear: 2011,
};
// to see the entire object we just call it: myComputer
// to see a specific part of it we write a dot and then the specific part: myComputer.screenSize
/*-------------------------------------*/
// 14 - Objects and Functions
/*
var nicco = {
name: "Nicco",
sayName: function sayName() {
console.log(this.name); // It'll display only "name"
}
*/
/*-------------------------------------*/
// 15 - Requirements
/*
- It should store the todos array on an object
- It should have a displayTodos method
- It should have a addTodo method
- It should have a changeTodo method
- It should have a delete method
*/
/*-------------------------------------*/
// 16 - It should store the todos array on an object
var todoList = {
todos: ["item 1", "item 2", "item 3"],
displayTodos: function() { // when functions are method (inside an object), they don't need to be named
console.log("My todos:", this.todos);
},
addtodo: function(todo) { // addTodo method
this.todos.push(todo);
this.displayTodos();
},
changeTodo: function(position, newValue) {
this.todos[position] = newValue;
this.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
this.displayTodos();
}
};
// What's great is that all these functions are grouped just in one object
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 4 - Booleans
// 17 - Requirements
/*
// todoList.addTodo should add objects
// todoList.changeTodo should should change the todoText property
// todoList.toggleCompleted should change the completed property
*/
/*-------------------------------------*/
var todoList = {
todos: [], // Preset items removed, substituted by objects added by addTodo below
displayTodos: function() { // when functions are method (inside an object), they don't need to be named
console.log("My todos:", this.todos);
},
addTodo: function(todoText) { // insted of todo, we are adding todoText, reffered to the object below
this.todos.push({
todoText: todoText, // The first is property, the second is a parameter, which can change
completed: false //It's a boolean value and it's false by default
}); // instead of todo, we are going to add an object{} with 2 properties
// to run: todoList.addTodo("some text") → It'll display an object inside of an array
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText; // This'll change just the todoText property, rather than the whole parameter
this.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) { // It needs a position to direct to
var todo = this.todos[position]; // it's a shortcut, insted of writing: this.todos[position].completed
todo.completed = !todo.completed; // we want the opposite
this.displayTodos();
//running ex.: todoList.toggleCompleted(0) → it'll switch true and false
}
};
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 5 - Loops of logic
// 18 - For loop
/*
i = 0
Say "hey" if i < 3
Increase i by 1
0 "hey"
1 "hey"
2 "hey"
*/
/*
for (initialization; condition; final-expression) {
console.log("hey");
}
for (var i = 0; i < 3; i++) {
console.log(i);
}
*/
/*-------------------------------------*/
// 19 - Looping over arrays
/*
var testArray = ["item 1", "item 2", "item 3"];
for (var i = 0; i < 3; i++) {
console.log(testArray[i]);
}
*/
// BUT using "3" works just in our case; better use testArray.length
// The length property will automatically change when n. of items changes
/*
So...
for (var i = 0; i < testArray.length; i++) {
console.log(testArray[i]);
}
*/
/*-------------------------------------*/
// 20 - Requirements
// .displayTodos should show .todoText
// .displayTodos should tell you if .todos is empty
// .displayTodos should show .completed
/*-------------------------------------*/
// 21 - .displayTodos should show .todoText
/*
.displayTodos should show .todoText property on each object in the .todos array
So we have to do some processing on each object in our .todos array
This is a perfect task for a "for loop"
*/
var todoList = {
todos: [], // Preset items removed, substituted by objects added by addTodo below
displayTodos: function() {
/*for(var i = 0; i < this.todos.length; i++) { // we want to continue for n. times al long as the array (this.todos) length
NB when we are on an array, we access very easily with []
we want to access the todoText property on each item in the array (inside addTodo())
console.log(this.todos[i].todoText); // to see it, we just console.log
}*/
// if there are no todos
// → if this.todos.length === 0;
if (this.todos.length === 0) {
console.log("Your todo list is empty!");
} else {
console.log("My todos:"); // this.todos eliminated, not so useful to see
// print todos as normal
// moved from above
for(var i = 0; i < this.todos.length; i++) {
// console.log(this.todos[i].todoText); removed to be put in the if statement
// check if .completed is true
if (this.todos[i].completed === true) {
// print with (x)
console.log("(x)", this.todos[i].todoText);
} else {
// print with ( )
console.log("( )", this.todos[i].todoText);
}
}
}
},
addTodo: function(todoText) { // insted of todo, we are adding todoText, reffered to the object below
this.todos.push({
todoText: todoText, // The first is property, the second is a parameter, which can change
completed: false //It's a boolean value and it's false by default
}); // instead of todo, we are going to add an object{} with 2 properties
// to run: todoList.addTodo("some text") → It'll display an object inside of an array
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText; // This'll change just the todoText property, rather than the whole parameter
this.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) { // It needs a position to direct to
var todo = this.todos[position]; // it's a shortcut, insted of writing: this.todos[position].completed
todo.completed = !todo.completed; // we want the opposite
this.displayTodos();
//running ex.: todoList.toggleCompleted(0) → it'll switch true and false
}
};
/*-------------------------------------*/
// 22 - displayTodos should tell you if todos is empty
// 23 - displayTodos should show .completed
var todoList = {
todos: [], // the array here is empty, so that the user can fill it up
displayTodos: function() {
// displayTodos should tell you if todos is empty
if (this.todos.length === 0) { // if there are no todos
console.log("Your todo list is empty!");
} else {
console.log("My todos:");
// for (initialization; condition; final expression)
// displayTodos should show .completed
for (var i = 0; i < this.todos.length; i++) {
if (this.todos[i].completed === true) {
console.log("(x)", this.todos[i].todoText);
} else {
console.log("( )", this.todos[i].todoText);
}
}
}
//print todos as normal
},
addTodo: function(todoText) {
this.todos.push({ // we are pushing an object, inside a function, inside an object
todoText: todoText, // we have the parameter and the argument that user is going to write
completed: false // it's false by default
});
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText; // we want the todoText property to be changed, not the whole object, so we put ".todoText"
this.displayTodos();
},
deleteTodo: function (position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
this.displayTodos();
}
};
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 6 - Thinking in code
// Requirements
// .toggleAll: if everything is true, make everything false
// toggleAll: otherwise make everything true
var todoList = {
todos: [], // the array here is empty, so that the user can fill it up
displayTodos: function() {
// displayTodos should tell you if todos is empty
if (this.todos.length === 0) { // if there are no todos
console.log("Your todo list is empty!");
} else {
console.log("My todos:");
// for (initialization; condition; final expression)
// displayTodos should show .completed
for (var i = 0; i < this.todos.length; i++) {
if (this.todos[i].completed === true) {
console.log("(x)", this.todos[i].todoText);
} else {
console.log("( )", this.todos[i].todoText);
}
}
}
//print todos as normal
},
addTodo: function(todoText) {
this.todos.push({ // we are pushing an object, inside a function, inside an object
todoText: todoText, // we have the parameter and the argument that user is going to write
completed: false // it's false by default
});
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText; // we want the todoText property to be changed, not the whole object, so we put ".todoText"
this.displayTodos();
},
deleteTodo: function (position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
this.displayTodos();
},
toggleAll: function(){
var totalTodos = this.todos.length;
var completedTodos = 0; // that's a safe assumption
// get number of completed todos
for (var i = 0; i < totalTodos; i++) {
if (this.todos[i].completed === true) {
completedTodos++;
}
}
// Case 1: if everything is true, make everything false
if(completedTodos === totalTodos) {
// make everything false
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = false;
}
} else {
// Case 2: otherwise make everything true
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = true;
}
}
this.displayTodos();
}
};
// we want to get access to the display todos button
var displayTodosButton = document.getElementById("displayTodosButton");
// we want to run displayTodos method, when someone clicks the "Display Todos" button
displayTodosButton.addEventListener("click", function(){
todoList.displayTodos();
});
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// INTERLUDE - Data Types Overview
/*
- Objects
{} arrays, functions
- Primitives (bulding blocks)
String
Number
Boolean
Undefined → value that hasn't been set
Null → nothing
Comparison with primitives (values)
1 === 1 → true
"blu" === "blu" → true
...
Comparison with objects (references)
{} === {} → false
[1,2,3] === [1,2,3] → false
It's like comparing the same houses with different addresses.
Memory addresses are always different.
*/
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 7 - HTML and the DOM
// Requirements
// There should be a displayTodo button and a toggleAll button in the app
// Clicking "Display Todos" should run todoList.displayTodos
// Clicking "Toggle All" should run todoList.toggleAll
var todoList = {
todos: [], // the array here is empty, so that the user can fill it up
displayTodos: function() {
// displayTodos should tell you if todos is empty
if (this.todos.length === 0) { // if there are no todos
console.log("Your todo list is empty!");
} else {
console.log("My todos:");
// for (initialization; condition; final expression)
// displayTodos should show .completed
for (var i = 0; i < this.todos.length; i++) {
if (this.todos[i].completed === true) {
console.log("(x)", this.todos[i].todoText);
} else {
console.log("( )", this.todos[i].todoText);
}
}
}
//print todos as normal
},
addTodo: function(todoText) {
this.todos.push({ // we are pushing an object, inside a function, inside an object
todoText: todoText, // we have the parameter and the argument that user is going to write
completed: false // it's false by default
});
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText; // we want the todoText property to be changed, not the whole object, so we put ".todoText"
this.displayTodos();
},
deleteTodo: function (position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
this.displayTodos();
},
toggleAll: function() {
var totalTodos = this.todos.length;
var completedTodos = 0; // that's a safe assumption
// get number of completed todos
for (var i = 0; i < totalTodos; i++) {
if (this.todos[i].completed === true) {
completedTodos++;
}
}
// Case 1: if everything is true, make everything false
if(completedTodos === totalTodos) {
// make everything false
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = false;
}
} else {
// Case 2: otherwise make everything true
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = true;
}
}
this.displayTodos();
}
};
// we want to get access to the display todos button
var displayTodosButton = document.getElementById("displayTodosButton");
// we want to run displayTodos method, when someone clicks the "Display Todos" button
displayTodosButton.addEventListener("click", function(){
todoList.displayTodos();
});
var toggleAllButton = document.getElementById("toggleAllButton");
toggleAllButton.addEventListener("click", function(){
todoList.toggleAll();
});
/*-------------------------------------*/
/*-------------------------------------*/
/*-------------------------------------*/
// VERSION 8
var todoList = {
todos: [], // the array here is empty, so that the user can fill it up
displayTodos: function() {
// displayTodos should tell you if todos is empty
if (this.todos.length === 0) { // if there are no todos
console.log("Your todo list is empty!");
} else {
console.log("My todos:");
// for (initialization; condition; final expression)
// displayTodos should show .completed
for (var i = 0; i < this.todos.length; i++) {
if (this.todos[i].completed === true) {
console.log("(x)", this.todos[i].todoText);
} else {
console.log("( )", this.todos[i].todoText);
}
}
}
//print todos as normal
},
addTodo: function(todoText) {
this.todos.push({ // we are pushing an object, inside a function, inside an object
todoText: todoText, // we have the parameter and the argument that user is going to write
completed: false // it's false by default
});
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText; // we want the todoText property to be changed, not the whole object, so we put ".todoText"
this.displayTodos();
},
deleteTodo: function (position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
this.displayTodos();
},
toggleAll: function() {
var totalTodos = this.todos.length;
var completedTodos = 0; // that's a safe assumption
// get number of completed todos
for (var i = 0; i < totalTodos; i++) {
if (this.todos[i].completed === true) {
completedTodos++;
}
}
// Case 1: if everything is true, make everything false
if(completedTodos === totalTodos) {
// make everything false
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = false;
}
} else {
// Case 2: otherwise make everything true
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = true;
}
}
this.displayTodos();
}
};
var handlers = {
displayTodos: function(){
todoList.displayTodos();
},
toggleAll: function(){
todoList.toggleAll();
}
};