-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
461 lines (357 loc) · 12.9 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="div1">the text above has been created dynamically</div>
<ul id="myList">
<li>coffee</li>
<li>Tea</li>
</ul>
<button onclick="myFunction()">Try Me</button>
<p class="myP">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore temporibus iste beatae. At facilis, corrupti autem possimus eos harum expedita animi nemo necessitatibus perferendis optio, accusamus enim minima dolorum. Explicabo?</p>
<button onclick="functionP()">P</button>
<h2>javascript addEventListener()</h2>
<button id="myBtn">Try It</button>
<p id="window"></p>
<script>
document.body.onload= addElement;
function addElement() {
const newDiv =document.createElement('div');
const newContent =document.createTextNode('hallo greetings');
newDiv.appendChild(newContent);
const currentDiv = document.getElementById('div1');
document.body.insertBefore(newDiv,currentDiv.nextSibling);
}
function myFunction() {
let node = document.createElement('li');
let textNode =document.createTextNode('Water');
node.appendChild(textNode);
document.getElementById('myList').appendChild(node)
}
function functionP() {
document.getElementsByClassName('myP')[0].style.backgroundColor = 'red';
document.getElementsByClassName('myP')[0].style.color = 'white';
document.getElementsByClassName('myP')[0].style.padding = '20px 10px';
}
let x = document.getElementById('myBtn');
x.addEventListener('click', myFunction2);
x.addEventListener('click', myFunction3);
function myFunction2() {
alert('EventListener');
}
function myFunction3() {
console.log('ini fungsi ke-2');
}
window.addEventListener('resize', function(){
document.getElementById('window').innerHTML = Math.random();
})
const buah= ['apel','jeruk','leci','lemon'];
console.log(buah.toString())
console.log(buah.join(' -'))
console.log(buah.pop())
console.log(buah)
console.log(buah.push('pisang'))
console.log(buah)
console.log(buah.shift())
console.log(buah)
console.log(buah.unshift('alpukat'))
console.log(buah)
buah[1] = 'anggur';
console.log(buah)
buah.splice(2,0,'kiwi','tomat') //ada juga slice()
buah.sort() // untuk urutin by abjad // ada juga reverse()
console.log(buah)
const myboy =['A','B']
const mygirls = ['C','D','E']
const myChildren = myboy.concat(mygirls)
console.log(myChildren)
const angka =[200,99,30,100,50,2,9,7,80];
angka.sort(function(a,b) {return a-b})
console.log(angka)
const cars = [
{type:'volvo', year:2006},
{type:'BMW', year:2010},
{type:'audi', year:2001}
]
console.log(cars.sort(function(a,b) {return a.year-b.year}))
////////////////////////////////////////////////////////////////
const angka2 =[200,99,30,100,50,2,9,7,80];
let baru = angka2.map(functionbaru);
// angka2.forEach(functionbaru);
function functionbaru(value, index, array) {
console.log(value)
}
////////////////////////////////////
const angka3= [1,3,5,8,31]
let newangka3 = [];
///////////////////////////////////////
angka3.forEach(i => newangka3.push(i * 2))
console.log(newangka3)
///////////////////////////////////
let filterr = angka3.filter((i) => i>=5 )
console.log(filterr)
///////////////////////////////////////
const d = new Date ()
// (2023, 0, 10, 22,15,0,0)
console.log(d.toUTCString())
console.log(d.toDateString())
console.log(d.toISOString())
//ada juga syntax every() dan some() untuk memeriksa value, returnnya boolean
//ada juga indexOf() dan find() yang mengembalikan nilai
// search()/ match()/ test() untuk mencari value dalam strings
// untuk search bisa di tambahkan regular expression modifier. yaitu
// i = untuk incase sensitif, g = global match(untuk mencari multiple value), dan m = untuk multiline match
///////////////////////////////////////
const checkAge = (x) => {
if (checkAge <= 17) {
return false
} return true;
}
console.log(checkAge(18))
/////////////////////////////////
//spread syntax
let numbers =[3,4]
let additionalNumbers = [1,2,...numbers,5,6];
console.log(additionalNumbers)
function sumThree(a,b,c){
return a+b+c
}
const threeNumber = [12,13,14]
console.log(sumThree(...threeNumber))
const obj1 = {
firstName : "Ega",
lastName : "Yudhistira"
}
const obj2 = {birthdate: "7 Maret 1997"}
const obj3 ={ ...obj1, ...obj2}
console.log(obj3)
//////////////////////////////////////////////////////
const numberss = [100,200,300]; //bisa juga pake string ='abcd';
const objek = {
firstName : "Ega",
lastName : "Yudhistira"
}
for (const num of numberss) //kalau for in untuk object dengan mengambil value kalau digunakan untuk array dia balikin index
{console.log(num)}
for (const datadiri in objek)
{console.log(objek[datadiri ])}
// for of lebih ke array/strings
//for in lebih ke object
///////////////////////////////////////////////
//Higher order funtion (reduce,map,filter,currying)
// function sum (nums) {
// let awal = 0;
// const output = nums.reduce((prev,current) => {
// console.log(prev)
// return prev+current
// },awal)
// }
// const angkaFP = [1,2,3,4,5];
// const totalsum = sum(angkaFP)
// console.log(totalsum)
const murid = [
{nama:'Ega', umur : 25 },
{nama:'Yudhistira', umur:25} ,
{nama:'Adam', umur:99} ,
{nama:'Hawa', umur:99 },
]
const dataMurid = murid.filter(mrd => mrd.umur < 26);
console.log(dataMurid)
const dataMurid2 = murid.map(mrd => mrd.umur * 2)
console.log(dataMurid2)
const dataMurid3 = murid.map((mrd) => `${mrd.nama} berusia ${mrd.umur}`)
console.log(dataMurid3)
const students = [
{nama:'Ega', umur : 25 },
{nama:'Yudhistira', umur:25} ,
{nama:'Adam', umur:99} ,
{nama:'Hawa', umur:99 },
]
const sumAges =students.reduce ((prev,current) => prev + current.umur,0);
const average =sumAges /students.length;
console.log(average)
const multiply = (a,b) => a*b;
console.log(multiply(2,3))
const curriedmultiply = (a) => (b) => a*b;
console.log(curriedmultiply(3)(3))
//contoh lain
const generateURL = (protocol) => (hostname) => (page) => `${protocol}${hostname}${page} `;
const httpmyskill = generateURL('https://')('www.myskill.id/');
const login = generateURL('/login');
const course = generateURL('/course');
const about = generateURL('/about');
//destructuring (assigmnet) untuk array dan object
// const hobbies = ['makan','tidur','musik'];
// const [first,second,third] = hobbies;
//disederhanakan jadi
const [first,second,third,fourth ='default'] = ['makan','tidur','musik'];
console.log(first)
const {name: carName,manufacture,speed} ={
name: 'civic',
manufacture:'honda',
speed:'180kmph'
}
console.log(manufacture,carName,' =',speed)
/////////////////////////////////
//constructor
function Person(name,age){
this.name =name,
this.age =age
}
const orang1= new Person ('Ega',25);
const orang2= new Person ('Yudhistira');
orang2.age =20;
console.log(orang2)
console.log(orang1)
const Persons = (newName,newAge) => {
this.name = newName;
this.age = newAge;
return {
name: this.name,
age: this.age
};
}
const xx = Persons("Egga",20)
console.log(xx.name + xx.age)
const People =() => {
this.name = 'Ega'
return {
name:this.name
}
}
const nama = People()
console.log(nama.name)
//// class
class Persona {
constructor(newName,newAge){
this.name = newName
this.age = newAge
}
}
const p = new Persona('Ega', 25)
console.log(p)
/////////class methode
class Orangs {
constructor(name){
this.name = name;
this.age = this.randomize()
}
randomize() {
return Math.floor(Math.random(100) * 100)
}
}
const p123 = new Orangs('You')
console.log(p123.age)
console.log(p123.randomize())
//menggunakan static : tidak perlu pake new: digunakan di luar scope
//menggunakan methode yang dimiliki oleh class
class Something {
static randomize() {
return Math.random(100) *100
}
static log(str) {
console.log(str)
}
}
Something.log(Something.randomize())
//getter
class Ppl {
#relationship
constructor(name,age,gender){
this.name = name;
this.age = age
this.gender = gender
this.#relationship ='in shambles'
}
get getName() { // kalau tanpa get bisa cuma invoked, harus di bikin kurung pas manggil
// getName() {
return ((this.gender === 'L' ? 'halo tuan ':'halo nyonya ')+this.name)
}
set setName(changedName){
this.name=changedName
}
get getRelationship() {
return this.#relationship
}
getRelation() {
return this.#relationship
}
}
const h = new Ppl('Ega', 25,'L')
const i = new Ppl('Dwi', 25,'P')
// console.log(h.getName()) kalau gapake pake get gini
console.log(h.getName)
console.log(i.getName)
///setter/// untuk mengubah nilai variable (code ikut yg atas)
h.setName = "Yudhistira"
console.log(h.getName)
//public and private property// cara aksesnya pake getter atau pake invoked ()
console.log(h.getRelationship)
console.log(h.getRelation())
//encapsulation supaya value tidak dapat di ubah sembarangan orang
class Ongkir {
#pajak
constructor(weight){
this.#pajak = 1000
this.biaya = this.calculateWeight(weight)
}
calculateWeight(weight) {
return weight * 2000;
}
get totalHarga() {
return this.#pajak + this.biaya
}
}
const ongkir = new Ongkir(20);
console.log(ongkir.totalHarga)
ongkir.pajak = 5000 // kalau gapake encapsulation maka orang bisa bebas ganti2 value pajak ini
console.log(ongkir.totalHarga)
//inheritance
class Peopler {
constructor(name,age){
this.name = name
this.age = age
this.job = 'jobless'
}
get getJob(){
return this.job
}
}
class PersonWithJob extends Peopler {
constructor(name,age,job){
super(name,age)
this.job =job
}
}
const you = new Peopler('you', 65)
const mih = new PersonWithJob('Me',25,'software Engineer')
console.log(you.getJob)
console.log(mih.getJob)
class Indonesian {
constructor(name){
this.name = name
}
greeting() {
return `Halo nama saya ${this.name}`
}
}
class English extends Indonesian {
constructor(name){
super(name)
}
greeting() {
return `Hi my name is ${this.name}`
}
}
const indonesian = new Indonesian('Ega')
const english = new English('yudhistira')
console.log(indonesian.greeting())
console.log(english.greeting())
</script>
</body>
</html>