-
Notifications
You must be signed in to change notification settings - Fork 0
/
terakhir.html
246 lines (169 loc) · 6.59 KB
/
terakhir.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
<script>
/*
__________________________________________
| |
=================== | Rangkuman SourceCode Javascript pemula | ====================================================
| untuk belajar sendiri |
| Bagian : terakhir |
| |
=================== | Made by : Viody Alfaridzi | ====================================================
|__________________________________________|
bisa di pelajari sendiri langsung via Freecodecamp.org
Persiapan :
1. Punya text editor; Visual Studio Code (recomended), Sublime, Atom, Notepad, dkk
2. Buka file ini di code editor n di browser (Chrome (recomended), google, dkk)
3. Inspect halaman web-nya, lalu ke console
*/
// ==================== Shortcut Function ================================
const hardCode = function (params) { // penggunaan function biasa
return new Date();
};
const ezCode = () => new Date(); // penggunaan function pakai shortcut
// = penggunaan shortcut function dgn array =
const gabung = (arr1, arr2) => arr1.concat(arr2); // fungsi ".concat()" adalah untuk menggabungkan 2 array
console.log(gabung([7, 5], [7, 6]));
// = mengatur nilai default dalam arrow shortcuts =
const increment = (function () {
return function increment(angka, nilai = 1) { // jadi jika tidak ada nilai yg di parsing maka default-nya adalah 1
return angka + nilai;
}
})();
console.log(increment(7, 2)); // nilai-nya 2
console.log(increment(7)); // secara default nilai-nya akan menjadi 1
// === Penggunaan Operator Sisa ===
// cara biasa
const sum = (
function () {
return function sum(x, y, z) {
const args = [x, y, z];
return args.reduce((a, b) => a + b, 0);
};
}
)();
console.log(sum(2, 2, 3));
// === menggunakan operator sisa ===
const tambah = (
function () {
return function tambah(...args) { // "...args" adalah operator sisa yang berfungsi memasukan berapapun nilai yg di parse kedalam "args"
return args.reduce((a, b) => a + b, 0);
};
}
)();
console.log(tambah(1, 2, 2));
// === spesial syntax destruktur ===
// gunanya buat mempersignkat kode
// misal :
// = cara biasa =
var koordinat = {
x: 7.5,
y: 7.6,
z: 7.3
};
var x = koordinat.x; // x = 7.5
var y = koordinat.y; // y = 7.6
var z = koordinat.z; // z = 7.3
// = menggunakan destruktur =
const { x: a, y: b, z: c } = koordinat; // a = 7.5 , b = 7.6 , c = 7.3
// = menggunakan destructur di nested array =
const TINGGI_BADAN = {
SMANSA: { min: 152, max: 175 },
SMADA: { min: 164, max: 180 }
};
function tinggiSmada(tinggi) {
const { SMADA: { max: maxSmada } } = tinggi;
return maxSmada;
}
console.log(tinggiSmada(TINGGI_BADAN));
// = memanipulasi isi array dgn ragam cara =
// misal :
const [p, q, , r] = [1, 2, 3, 4, 5, 6];
console.log(p, q, r);
// atau dengan cara
let e = 7, f = 5;
(() => {
[e, f] = [f, e]
})();
console.log(e, f);
// cara lainnya :
const awal = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
function hapusDuaDiawal(list) {
const [, , ...arr] = list; // menghilangkan 2 urutan array pertama
return arr;
};
const ubah = hapusDuaDiawal(awal);
console.log(awal);
console.log(ubah);
// = koding challenge freecodecamp = manipulasi array dgn simbol backticks =
const result = {
sukses: ["var1", "var2", "var3"],
gagal: ["var4", "var5", "var6"],
skip: ["var7", "var8", "var9"]
};
function buatList(data) {
const hasilDisplay = [];
for (let i = 0; i < data.length; i++) {
hasilDisplay.push(`<li class="text-warning"> ${data[i]}</li>`)
}
return hasilDisplay;
}
const hasilDisplay = buatList(result.gagal);
console.log(hasilDisplay);
// = manipulasi literal =
// cara biasa :
const manusia = (nama, umur, gender) => {
return {
nama: nama,
umur: umur,
gender: gender
};
};
console.log(manusia("dibleton", 100, "unknown"));
// cara singkat
const manusia1 = (nama, umur, gender) => ({ nama, umur, gender });
console.log(manusia1("wigleton", 70, "unknown"));
// ======================= Penggunaan SYNTAX CLASS ===================================
var spaceShuttle = function (targetPlanet) { // fungsi untuk menyimpan apa yg di parse pada fungsi ke "targetPlanet"
this.targetPlanet = targetPlanet;
}
let zeus = new spaceShuttle('jupiter');
let midas = new spaceShuttle('bumi');
console.log(zeus.targetPlanet); // coba ganti zeus ke midas
// cara mudahnya :
class satelit { // menggunakan syntax class
constructor(planetTarget) {
this.planetTarget = planetTarget;
}
}
let bulan = new satelit('bumi'); // maksudnya "jadikan bulan bagian dari satelit di planet bumi"
console.log(bulan.planetTarget); // jadi ketika var bulan menggunakan fungsi "planetTarget" maka JS akan mengembalikan nilai "bumi"
// ============= menggunakan Getter dan Setter =============================
// getter berguna untuk mengambil suatu value dalam class object
// setter berguna untuk mengeset variabel tersebut ke dalam objek
function buatClass() {
class thermostat {
constructor(temp) {
this._temp = 5 / 9 * (temp - 32);
}
get temperatur() {
return this._temp;
}
set temperatur(updatedTemp) {
this._temp = updatedTemp;
}
}
return thermostat;
}
const thermostat = buatClass;
const thermos = new thermostat(75);
let temp = thermos.temperatur;
thermos.temperatur = 25;
temp = thermos.temperatur;
console.log(temp);
// ====================== IMPORT n EXPORT ==============================================
// import { kapital } from "./fungsiKapital.js";
// const kap = kapital("hello the world");
// console.log(kap);
// jika mengimpor banyak item dapat menggunakan syntax *
// misal :
// import * as nama from " ./nama_file ";
</script>