-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.js
More file actions
158 lines (124 loc) · 3.86 KB
/
tutorial.js
File metadata and controls
158 lines (124 loc) · 3.86 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
// var , let , const
//var is globally scoped
//let you can reassign values
//const means it cannot be changed
//always use const unless you know you are gonna reassign it to make your code more robust
//Data types => Strings, numbers, boolean, null, undefined
const name = 'John';
const num = 19;
const isCool = true;
const rating = 4.5; //there is not float data type in js, it is just a number
const x = null;
const y = undefined;
//console.log(typeof y);
//object literals
const person = {
firstName: 'Hilbert',
lastName: 'Muchatibaya',
age: 30,
hobbies: ['Music','Cricket','DC Movies'],
address: {
street: '50 main st',
city: 'Boston',
state: 'MA'
}
}
for (i = 0; i < person.hobbies.length; i++) {
// console.log('Hobbies:' + person.hobbies[i]);
}
//console.log(' I come from the city of ' + person.address.city + ' in the state of ' + person.address.state);
//pulling different things out from an object i.e destructuring
const {firstName , lastName, address: {state}} = person;
//console.log(firstName + lastName + state);
//Creating a todo list
const toDo = [
{
id: 1,
tasks: 'Clean up room',
isCompleted: true
},
{
id: 2,
tasks: 'Clean up desk',
isCompleted: false
},
{
id: 1,
tasks: 'Clean up home',
isCompleted: true
},
];
let newList = [];
let c = 0;
for(i = 0; i < toDo.length; i++){
if(toDo[i].isCompleted){
continue;
}
else {
newList[c] = toDo[i];
c++;
}
}
//console.log(newList[0].tasks);
//JSON used alot when sending data to a server. Its very similar to object literals
//freeformatter.com to convert object literals to JSON
//converting within a script
const todoJSON = JSON.stringify(toDo);
//console.log(todoJSON);
//higher order array method
//foreach, map, filter
//toDo.forEach((toDo) => console.log(toDo.tasks) );
//map, takes in a function that transforms the data and then maps it
const toDoarray = toDo.map((toDo) => { if(toDo.id == 1) {return "This is the one" } else return toDo.tasks + " This is not high priority"});
//console.log(toDoarray);
const filteredList = toDo.filter((toDo) => { return toDo.isCompleted === false }).map((toDo) => {return toDo.tasks});
//console.log(filteredList);
//Conditionals ? === if : === else
//constructive objects
function Person(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = new Date(dob);
}
//instatiate object
const person1 = new Person ('Hilbert', 'Mooch', '15-09-1995');
//Class
class People {
constructor(firstName, lastName, dob){
this.firstName = firstName;
this.lastName = lastName;
this.dob = new Date(dob);
}
getFullname(){
return this.firstName + ' ' + this.lastName;
}
}
//instantiate
const person3 = new People ('Hilbert','Mooch','15-09-1995');
//console.log(person3.getFullname());
//the DOM! The DOM is a tree structure of your whole document
//selecting elements from the DOM
//Single element selectors and multiple element selectors
const form = document.getElementById('my-form');
const form1 = document.querySelector('h1');
//console.log(form1);
//Manipulating the DOM
const ul = document.querySelector('.items');
ul.firstElementChild.textContent = 'Look at Me';
const btn = document.querySelector('.btn');
const email = document.querySelector('#email');
const name1 = document.querySelector('#name');
const msg = document.querySelector('.msg');
btn.addEventListener('click', (e) => {
if(email.value === "" || name1.value === ""){
e.preventDefault();
btn.style.background = 'green';
msg.classList.add('error');
msg.innerHTML = 'Please Enter All Fields';
}
else
{
btn.style.background = 'red';
alert('Success');
}
});