├── Array.js ├── Concurrency.js ├── Function.js ├── FunctionalProgramming.js ├── LogikaOperatorDanIfElse.js ├── Map.js ├── Module ├── Tiger.js ├── Wolf.js └── main.js ├── Object.js ├── ObjectOrientedProgramming.js ├── PenangananEror.js ├── README.md └── VariabelDanTipeData.js /Array.js: -------------------------------------------------------------------------------- 1 | let evenNumber = []; 2 | for (let i = 2; i <= 100; i++) { 3 | evenNumber.push(i); 4 | i++; 5 | } 6 | console.log(evenNumber); -------------------------------------------------------------------------------- /Concurrency.js: -------------------------------------------------------------------------------- 1 | class NetworkError extends Error { 2 | constructor(message) { 3 | super(message); 4 | this.name = 'NetworkError'; 5 | } 6 | } 7 | 8 | const fetchingUserFromInternet = (isOffline) => { 9 | return new Promise((resolve, reject) => { 10 | setTimeout(() => { 11 | if (!isOffline) { 12 | resolve({ name: 'John', age: 18 }); 13 | } else { 14 | reject(new NetworkError('Gagal mendapatkan data dari internet')); 15 | } 16 | }, 500); 17 | }); 18 | }; 19 | 20 | 21 | async function gettingUserName() { 22 | try { 23 | const fetchUser = await fetchingUserFromInternet(false); 24 | return fetchUser.name; 25 | } catch (NetworkError) { 26 | return NetworkError; 27 | } 28 | }; 29 | 30 | gettingUserName(); -------------------------------------------------------------------------------- /Function.js: -------------------------------------------------------------------------------- 1 | function minimal(a, b) { 2 | if (a < b) { 3 | return a; 4 | } else if (a > b) { 5 | return b; 6 | } else return a; 7 | }; 8 | 9 | function power(a, b) { 10 | return a ** b; 11 | }; 12 | 13 | console.log(minimal(3, 4)); 14 | console.log(power(4, 2)); -------------------------------------------------------------------------------- /FunctionalProgramming.js: -------------------------------------------------------------------------------- 1 | const books = [ 2 | { title: 'The Da Vinci Code', author: 'Dan Brown', sales: 5094805 }, 3 | { title: 'The Ghost', author: 'Robert Harris', sales: 807311 }, 4 | { title: 'White Teeth', author: 'Zadie Smith', sales: 815586 }, 5 | { title: 'Fifty Shades of Grey', author: 'E. L. James', sales: 3758936 }, 6 | { title: 'Jamie\'s Italy', author: 'Jamie Oliver', sales: 906968 }, 7 | { title: 'I Can Make You Thin', author: 'Paul McKenna', sales: 905086 }, 8 | { title: 'Harry Potter and the Deathly Hallows', author: 'J.K Rowling', sales: 4475152 }, 9 | ]; 10 | 11 | const filter = books.filter((book) => book.sales > 1000000); 12 | const greatAuthors = filter.map((buku) => { return `${buku.author} adalah penulis buku ${buku.title} yang sangat hebat!` }); 13 | console.log(greatAuthors); -------------------------------------------------------------------------------- /LogikaOperatorDanIfElse.js: -------------------------------------------------------------------------------- 1 | function scoreChecker(score) { 2 | let result; 3 | 4 | if (score >= 90) { 5 | result = "Selamat! Anda mendapatkan nilai A."; 6 | } else if (score >= 80 && score <= 89) { 7 | result = "Anda mendapatkan nilai B."; 8 | } else if (score >= 70 && score <= 79) { 9 | result = "Anda mendapatkan nilai C."; 10 | } else if (score >= 60 && score <= 69) { 11 | result = "Anda mendapatkan nilai D."; 12 | } else result = "Anda mendapatkan nilai E."; 13 | 14 | 15 | return result; 16 | } -------------------------------------------------------------------------------- /Map.js: -------------------------------------------------------------------------------- 1 | const priceInJPY = 5000; 2 | 3 | const currency = new Map([ 4 | ["USD", 14000], 5 | ["JPY", 131], 6 | ["SGD", 11000], 7 | ["MYR", 3500], 8 | ]); 9 | 10 | let priceInIDR = 5000 * currency.get("JPY"); 11 | console.log(priceInIDR); -------------------------------------------------------------------------------- /Module/Tiger.js: -------------------------------------------------------------------------------- 1 | class Tiger { 2 | constructor() { 3 | this.strength = Math.floor(Math.random() * 100); 4 | } 5 | 6 | growl() { 7 | return 'grrrrrrr'; 8 | } 9 | } 10 | 11 | module.exports = Tiger; -------------------------------------------------------------------------------- /Module/Wolf.js: -------------------------------------------------------------------------------- 1 | class Wolf { 2 | constructor() { 3 | this.strength = Math.floor(Math.random() * 100); 4 | } 5 | 6 | howl() { 7 | return 'Auuuuuuuuu'; 8 | } 9 | } 10 | 11 | module.exports = Wolf; -------------------------------------------------------------------------------- /Module/main.js: -------------------------------------------------------------------------------- 1 | const Tiger = require('./Tiger'); 2 | const Wolf = require('./Wolf'); 3 | 4 | const fight = (tiger, wolf) => { 5 | if (tiger.strength > wolf.strength) { 6 | return tiger.growl(); 7 | } 8 | if (wolf.strength > tiger.strength) { 9 | return wolf.howl(); 10 | } 11 | return 'Harimau dan serigala sama-sama kuat!'; 12 | }; 13 | 14 | const myTiger = new Tiger(); 15 | const myWolf = new Wolf(); 16 | 17 | const result = fight(myTiger, myWolf); -------------------------------------------------------------------------------- /Object.js: -------------------------------------------------------------------------------- 1 | const restaurant = { 2 | name: "Bryan", 3 | city: "Surabaya", 4 | "favorite drink": "Beer", 5 | "favorite food": "chicken", 6 | isVegan: false 7 | }; 8 | 9 | let name; 10 | let favoriteDrink 11 | name = restaurant.name; 12 | favoriteDrink = restaurant["favorite drink"]; 13 | 14 | console.log(name); 15 | console.log(favoriteDrink); -------------------------------------------------------------------------------- /ObjectOrientedProgramming.js: -------------------------------------------------------------------------------- 1 | class Animal { 2 | constructor(name, age, isMammal) { 3 | this.name = name; 4 | this.age = age; 5 | this.isMammal = isMammal; 6 | } 7 | } 8 | 9 | class Rabbit extends Animal { 10 | constructor(name, age, isMammal = true) { 11 | super(name, age, isMammal); 12 | } 13 | 14 | eat() { 15 | return `${this.name} sedang makan!`; 16 | } 17 | } 18 | 19 | class Eagle extends Animal { 20 | constructor(name, age, isMammal = false) { 21 | super(name, age, isMammal); 22 | } 23 | 24 | fly() { 25 | return `${this.name} sedang terbang!`; 26 | } 27 | } 28 | 29 | const myRabbit = new Rabbit("Labi", 2); 30 | const myEagle = new Eagle("Elo", 4); 31 | 32 | console.log(myRabbit.eat()); 33 | console.log(myEagle.fly()); -------------------------------------------------------------------------------- /PenangananEror.js: -------------------------------------------------------------------------------- 1 | class ValidationError extends Error { 2 | constructor(message) { 3 | super(message); 4 | this.name = "ValidationError"; 5 | } 6 | } 7 | 8 | const validateNumberInput = (a, b, c) => { 9 | if (typeof a != "number") { 10 | throw new ValidationError("Argumen pertama harus number"); 11 | } else if (typeof b != "number") { 12 | throw new ValidationError("Argumen kedua harus number"); 13 | } else if (typeof c != "number") { 14 | throw new ValidationError("Argumen ketiga harus number"); 15 | } 16 | } 17 | 18 | const detectTriangle = (a, b, c) => { 19 | try { 20 | validateNumberInput(a, b, c); 21 | } catch (error) { 22 | return `${error.message}`; 23 | } 24 | 25 | if (a === b && b === c) { 26 | return 'Segitiga sama sisi'; 27 | } 28 | 29 | if (a === b || a === c || b === c) { 30 | return 'Segitiga sama kaki'; 31 | } 32 | 33 | return 'Segitiga sembarang'; 34 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dasar Pemrograman Javascript 2 | 3 | ## English 4 | This is a repository that is used to accommodate Javascript code in the "Learn Basic JavaScript Programming" class. Dicoding, to see the desired file, you can go directly to the folder according to the desired section. 5 | 6 | ## Bahasa 7 | Ini adalah repository yang digunakan untuk menampung kode Javascript pada kelas "Belajar Dasar Pemrograman JavaScript" Dicoding, untuk melihat file yang diinginkan bisa langsung menuju ke folder sesuai dengan bagian yang diinginkan 8 | -------------------------------------------------------------------------------- /VariabelDanTipeData.js: -------------------------------------------------------------------------------- 1 | const firstName = "Bryan"; 2 | const lastName = "Yehuda"; 3 | const age = 21; 4 | const isMarried = false; --------------------------------------------------------------------------------