diff --git a/1-fundamental-task.js b/1-fundamental-task.js new file mode 100644 index 0000000..625471f --- /dev/null +++ b/1-fundamental-task.js @@ -0,0 +1,41 @@ +'use strict' +// Task 1 + +let admin, name; +name = "John"; +admin = name; +console.log(admin); + +// Task 2 + +const BIRTHDAY = '1990-12-17'; +const AGE = someCode(BIRTHDAY); +console.log(AGE); + +function someCode(value){ + let KONVERTER = 1000*60*60*24*365; + let tanggal = new Date(value).getTime(); + let today = new Date().getTime(); + let age = (today-tanggal)/KONVERTER; + return Math.floor(age); +} + +// Task 3 : data type +let nama = 'jokontowl'; +// alert(`helo ${1}`); // gak work running di terminal karna alert itu fungsi di browser +// output : helo 1 // harus run di browser lewat console atau diembed ke html + +// alert(`helo ${'nama'}`); +// output : helo nama + +// alert(`helo ${nama}`); +// output : halo jokontowl + + +// task 4 : interaksi +/* + +let yourName = prompt('What is your name :'); +confirm(yourName); + +*/ \ No newline at end of file diff --git a/1a-fundamental-hello.html b/1a-fundamental-hello.html index 2b5a076..21d9a22 100644 --- a/1a-fundamental-hello.html +++ b/1a-fundamental-hello.html @@ -7,6 +7,6 @@ - + \ No newline at end of file diff --git a/1b-fundamental-var.js b/1b-fundamental-var.js index 5f212c7..e8d814c 100644 --- a/1b-fundamental-var.js +++ b/1b-fundamental-var.js @@ -7,7 +7,7 @@ tong = 400; // True // let tong = 399; // false bcoz redeclare tong // var adalah keyword sama dg let -// var adalah bentuk old +// var adalah bentuk old- legacy way. leave it // variable naming @@ -24,6 +24,10 @@ console.log(_); // asignment using reserved words // let let = 30; +// wrong brooooo +// let return = 12; +// console.log(return); +// output : unexpected strict mode reserved word "return" // numm = 5; // able not asignment using let in old time, but unable if used in strict mode // console.log(numm); //referenceError @@ -40,4 +44,8 @@ console.log(colorku); // you cant re-declare a constant const KONST = 19.35; // KONST = 20; redeclaring is forbidden for const -console.log(KONST); \ No newline at end of file +console.log(KONST); + +// actually constants used for aliases the hard-coded value +// daripada pakai value #F00, mending simpan value tsb ke COLOR_RED +// untuk menghindari misstype saat memanggil berulang-ulang. \ No newline at end of file diff --git a/1c-fundamental-dataTypes.js b/1c-fundamental-dataTypes.js new file mode 100644 index 0000000..46dbef8 --- /dev/null +++ b/1c-fundamental-dataTypes.js @@ -0,0 +1,103 @@ +'use strict' +// 8 basic Data Type +// tipe data disebut 'primitive', karena nilainya hanya berisi 1 jenis +// tidak dengan object yang simpan data beragam + +// a. number +let n = 123; +console.log(n); +// present both integer n floating +let infini = 1/0; +console.log(infini); // output : infinity + +//NaN represent computational error and it is sticky no matter- +// what you do math operation +console.log("not number" / 2); // output : NaN +console.log('NaN + 1 =', NaN + 1); +console.log('pengecualian : NaN ** 0 =', NaN ** 0); + +// - - - - - - - - - - - - - - - - // + + +// b. BigInt + +// when number cant represent values larger than 2^53 -1 +// we need store larger +// check this anomaly : +let batasInt = (2**53)-1 +console.log('batas int', batasInt); +console.log(9007199254740991 + 1); // 9007199254740992 +console.log(9007199254740991 + 2); // 9007199254740992 +console.log(9007199254740991 + 100); // 9007199254740992 +// Output are the same, bcoz these reach the limit of storage +// we need to use BigInt by adding "n" to the end of integer +let angkaBigInt = 90071992547410929999n; +console.log(angkaBigInt); + +// - - - - - - - - - - - - - - - - // + + +// c. String + +let namaUser = "joekoe"; // double quotes +let namaOrang = 'koejoe'; // single quotes +console.log(namaUser, namaOrang); // double n single quotes have no difference in output console +console.log(`mereka adalah ${namaUser} dan ${namaOrang}`); // backtick for template literal +console.log(`contoh arithmetical expression ${300+99}`); // backtick bs diisi oprasi mat + +// - - - - - - - - - - - - - - - - // + +// d. Boolean + +// instance boolean true false +let isChecked = false; +console.log(isChecked); + +// atau hasil komparasi +let isBesar = 4<1; +console.log('4 < 1 :', isBesar); + + +// - - - - - - - - - - - - - - - - // + +// e. null and undefined + +// nilai null : refers to nothing,empty,unknown +let nilaiNull = null; +console.log(nilaiNull); + +// nilai undefined : means value isnt assigned +// juga berlaku untuk variabel yang di-declare tapi tidak diberi nilai +let nilaiUndefined; +console.log(nilaiUndefined); +console.log(null == undefined); // wtf +console.log(null === undefined); //wtf + +// - - - - - - - - - - - - - - - - // + +// f. object dan simbol + +// dibahas soon + + +// - - - - - - - - - - - - - - - - // + +// g. tipe operator (typeof) +// sebuah operator yang mengembalikan tipe suatu operand. +console.log('tipe undefined :', typeof(undefined)); +console.log('tipe 99 :', typeof(99)); +console.log('tipe 10n :', typeof(10n)); +console.log('tipe true :', typeof(true)); +console.log('tipe \'halo\' :', typeof('halo')); +console.log('tipe Math :', typeof(Math)); // math adalah built-in objek utk operasi matematika +console.log('tipe null :', typeof(null)); // wtf null adalah object. ini anomali di JS utk kompatibilitas saja +console.log('tipe console:', typeof(console)); +console.log('tipe alert:', typeof(alert)); +console.log(typeof(alert)); + +// nb : typeof isnot a function but operator +// kita bs pakai typeof : typeof string +console.log(typeof 'hahay'); +// dan juga typeof(string) + +// - - - - - - - - - - - - - - - - // diff --git a/1c-fundamental-task.js b/1c-fundamental-task.js deleted file mode 100644 index ccf9f24..0000000 --- a/1c-fundamental-task.js +++ /dev/null @@ -1,12 +0,0 @@ -// Task 1 - -let admin, name; -name = "John"; -admin = name; -console.log(admin); - -// Task 2 - -const BIRTHDAY = '18.04.1982'; -const AGE = someCode(BIRTHDAY); -console.log(AGE); \ No newline at end of file diff --git a/1d-fundamental-interaction.js b/1d-fundamental-interaction.js new file mode 100644 index 0000000..1dbc95c --- /dev/null +++ b/1d-fundamental-interaction.js @@ -0,0 +1,30 @@ +// Interaksi : alert, prompt, confirm + +// alert +// hanya jalan di browser. karna pop up mini window(modal window) +// modal means : user gbs ngapa-ngapain sblm deal dengan window, contoh : tekan 'OK' +// alert('helo alert'); + +// - - - - - - - - - - - - - - - - // + +// prompt +// fungsi yang terima 2 argument +/* + +let result = prompt('umur berapa?',100); // fungsi hanya berjalan di browser jg. +console.log(`usia anda ${result}`); + +*/ + +// - - - - - - - - - - - - - - - - // + +// confirm +// fungsi confirm mengeluarkan modal window juga dg 2 tombol: OK & Cancel +/* + +let isBoss = confirm('are you the boss?'); // hanya jalan di browser +alert(isBoss); + +*/ + +// - - - - - - - - - - - - - - - - // diff --git a/1e-typeconversion.js b/1e-typeconversion.js new file mode 100644 index 0000000..f2ccedc --- /dev/null +++ b/1e-typeconversion.js @@ -0,0 +1,59 @@ +// Type conversion + +// sepanjang waktu, operator dan fungsi melakukan konversi nilai secara otomatis +// yang diinputkan ke mereka menjadi nilai yang sesuai +// contoh: alert akan konversi nilai apapun menjadi string agar bs ditampilkan di layar + +// konversi String +// dipakai ketika kita ingin bentuk string +let values = true; +console.log('tipe values-before:', typeof values); +// mari diubah ke string +values = String(values); +console.log('tipe values-after:', typeof values); + +// ----------------------------------------- // + +// konversi Numeric +// konversi ini otomatis disaat operasi math. +//contoh operasi math ke non-numbers + +console.log('4'/'2'); // amazing wak baru tau wkwkw. kaco ni JS + +// untk eksplisit convert bisa pake fungsi Number() +let stringAngka = '123'; +let stringToAngka = Number(stringAngka); +console.log(stringAngka,'tipe: ',typeof stringAngka); +console.log(stringToAngka,'tipe: ',typeof stringToAngka); +console.log('konversi undefined: ', Number(undefined)); + +// konversi diperlukan terutama jika nilai tsb berasal dari form teks +// yang diinputkan adalah string, sedangkan kita mengharapkan nilai itu adalah number +// maka perlu dikonversi + +// jika kita konversi string yang bukan angka valid, hasilnya adalah NaN +let teks = 'jokonto'; +let teksToNumber = Number(teks); +console.log(teksToNumber); + +// konversi rules: +// undefined -> NaN +// null -> 0 +// true false -> 1 0 +// '' -> 0 +console.log(Number('')); // output: 0 + +// ----------------------------------------- // + +// konversi boolean + +// konversi rules : +// value kosong, 0, '',null, undefined,NaN -> false +// sisanya - > true + +console.log(Boolean(1)); +console.log(Boolean(0)); +console.log(Boolean('halo')) +console.log(Boolean('')); + +// ----------------------------------------- // \ No newline at end of file diff --git a/1f-basicOp.js b/1f-basicOp.js new file mode 100644 index 0000000..7602719 --- /dev/null +++ b/1f-basicOp.js @@ -0,0 +1,125 @@ +// Dasar operator matematik + +// Istilah unary, binary,operand + +// operand : objek yang dikenai operasi matematik +// contoh : 2+3 +// 2 & 3 adalah operand, + adalah operator +// operand alias argumen + +// remainder % (sisa bagi) +// di javascript, % bukan persen melainkan operator sisa bagi +// contoh : +console.log(5%2); // 5/2 = 2 sisa 1 maka jawabannya: 1 +console.log(8%3); // output :2 + +// exponential * (pangkat) +// berlaku juga untuk pangkat pecahan atau akar pangkat +console.log('akar pangkat 4: ', 4**(1/2)) + +// ------------------------- // + +// String concatenation dg binary + + +//contoh : +let s = 'my' + 'string'; +console.log(s); // output : mystring +// nb : jika salah satu operand adalah string, maka operand lain dikonversi jd string +console.log(1 + '9'); // output : 19 +console.log('1' + 9); // output : 19 + +// jika operator '+' berusaha membuat operand menjadi string +// tidak halnya dengan operator yang lain, seperti '-' + +console.log(6-'1'); // output: 5 +console.log('4'/'2'); // output: 2 + +// ------------------------- // + +// unary + + +// unary + bisa konvert sesuatu jadi angka +console.log(+true); // output : 1 +console.log(+''); // output : 0 +// kyk fungsi Number (wah shorthand nich) +// hal ini bs dipakai untuk konvert string jadi number +// kebanyakan hasil dari inputan form HTML defaultnya adalah string +let nilai1 = '2'; +let nilai2 = '8'; +console.log(+nilai1 + +nilai2); // output: 10 (sesuai keinginan kita) +console.log(Number(nilai1) + Number(nilai2)); // output: 10 (versi panjang dari unary +) + +// ------------------------- // + +// Prioritas operator + +/* +(urutan teratas s.d terbawah) + +[ unary +, unary - ] +[ ** ] +[ *, / ] +[ +, - ] + +*/ + +// ------------------------- // + +// chaining assignment + +// kita dapat melakukan assignment secara berantai +// contoh : +let a,b,c; +a=b=c= 99; +console.log(a,b,c); + +// untuk keterbacaan kode, sbaiknya displit menjadi : +let d = 212; +a = d; +b = d; +c = d; +console.log(a,b,c,d); + +// ------------------------- // + + +// modif var ( timpa var) +// contoh : +let timpa = 39; +timpa = timpa + 69; +console.log(timpa); // kita ingin mengubah variabel tanpa menambah variabel baru (modif var yg exist) +// misal kita buat var baru dan copy dari timpa +let tambal = timpa; +tambal += 690; // penulisan ini sama dengan tambal = tambal + 690 +console.log(tambal); +// contoh lain : +tambal -= 600; +console.log(tambal); +tambal *= 0.01; +console.log(tambal); + +// ------------------------- // + +// increment/ decrement + +// ++ increase by 1 +// -- decrease by 1 +let counter = 4; +counter ++; +console.log(counter); // output : 5 +counter --; +console.log(counter); // output : 4 + +// nb : increment dan decrement hanya berlaku utk variabel, tidak utk value +// nb : postfix form -> counter++ +// nb : prefix form -> ++counter +// bedanya terlihat pada contoh berikut + +// prefix +let ticker = 1; +let kicker = 1; +let x = ++ticker; +console.log('nilai pre-fix', x); // nilai x terupdate +1 menjadi 2 +let y = kicker++; +console.log('nilai post-fix', y); // nilai y masih 1, justru kickernya yang 1+1 +console.log('nilai kicker', kicker); \ No newline at end of file diff --git a/2a-fundamental-dataTypes.js b/2a-fundamental-dataTypes.js deleted file mode 100644 index 86e93b8..0000000 --- a/2a-fundamental-dataTypes.js +++ /dev/null @@ -1,42 +0,0 @@ -// 8 basic Data Type - -// a. number -let n = 123; -console.log(n); -// present both integer n floating -let infini = 1/0; -console.log(infini); // output : infinity - -//NaN represent computational error and it is sticky no matter- -// what you do math operation -console.log("not number" / 2); // output : NaN - -// - - - - - - - - - - - - - - - - // - - -// b. BigInt - -// when number cant represent values larger than 2^53 -1 -// we need store larger -// check this anomaly : -console.log(9007199254740991 + 1); // 9007199254740992 -console.log(9007199254740991 + 2); // 9007199254740992 -console.log(9007199254740991 + 100); // 9007199254740992 -// Output are the same, bcoz these reach the limit of storage -// we need to use BigInt by adding "n" to the end of integer -let angkaBigInt = 90071992547410929999n; -console.log(angkaBigInt); - -// - - - - - - - - - - - - - - - - // - - -// c. String - -let namaUser = "joekoe"; // double quotes -let namaOrang = 'koejoe'; // single quotes -console.log(namaUser, namaOrang); // double n single quotes have no difference in output console -console.log(`mereka adalah ${namaUser} dan ${namaOrang}`); // backtick for template literal - -// - - - - - - - - - - - - - - - - // - -// d. Boolean \ No newline at end of file