diff --git a/.eslintrc.json b/.eslintrc.json index 6daa120..1a1ad8d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,15 +1,12 @@ { - "env": { - "commonjs": true, - "es2021": true, - "node": true - }, - "extends": [ - "airbnb-base" - ], - "parserOptions": { - "ecmaVersion": 12 - }, - "rules": { - } + "env": { + "commonjs": true, + "es2021": true, + "node": true + }, + "extends": ["airbnb-base"], + "parserOptions": { + "ecmaVersion": 12 + }, + "rules": {} } diff --git a/README.md b/README.md index 1e85b1a..b2f9a36 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # javascript-programming + > Javascript codes to solve Dicoding quizzes. + - [Content](#Content) - [Author](#Author) - [Reference](#Reference) ## Content + - [Variable and Data Type](src/variable-and-data-type) - [Logic Operator and If Else](src/logic-operator-and-if-else) - [Object](src/object) @@ -18,7 +21,9 @@ - [Concurrency](src/concurrency) ## Author + [Mgs. Tabrani](https://github.com/mgstabrani) ## Reference + [Dicoding](https://www.dicoding.com/) diff --git a/src/array/main.js b/src/array/main.js index 906bee7..aabeab5 100644 --- a/src/array/main.js +++ b/src/array/main.js @@ -8,13 +8,13 @@ */ // TODO -const evenNumber = []; +const evenNumber = [] for (let i = 1; i <= 50; i += 1) { - evenNumber.push(i * 2); + evenNumber.push(i * 2) } /** * Hiraukan kode di bawah ini */ -module.exports = evenNumber; +module.exports = evenNumber diff --git a/src/concurrency/NetworkError.js b/src/concurrency/NetworkError.js index 3bab575..c7eade7 100644 --- a/src/concurrency/NetworkError.js +++ b/src/concurrency/NetworkError.js @@ -1,8 +1,8 @@ class NetworkError extends Error { - constructor(message) { - super(message); - this.name = 'NetworkError'; + constructor (message) { + super(message) + this.name = 'NetworkError' } } -module.exports = NetworkError; +module.exports = NetworkError diff --git a/src/concurrency/main.js b/src/concurrency/main.js index b26aa18..14d624c 100644 --- a/src/concurrency/main.js +++ b/src/concurrency/main.js @@ -30,32 +30,33 @@ * - Tetap gunakan NetworkError untuk membawa nilai error pada Promise */ -const NetworkError = require('./NetworkError'); +const NetworkError = require('./NetworkError') // TODO: 1 -const fetchingUserFromInternet = (isOffline) => new Promise((resolve, reject) => { - setTimeout(() => { - if (!isOffline) { - resolve({ name: 'John', age: 18 }); - } else { - reject(new NetworkError('Gagal mendapatkan data dari internet'), null); - } - }, 500); -}); +const fetchingUserFromInternet = (isOffline) => + new Promise((resolve, reject) => { + setTimeout(() => { + if (!isOffline) { + resolve({ name: 'John', age: 18 }) + } else { + reject(new NetworkError('Gagal mendapatkan data dari internet'), null) + } + }, 500) + }) // TODO: 2 -async function gettingUserName() { +async function gettingUserName () { try { - const user = await fetchingUserFromInternet(false); - return user.name; + const user = await fetchingUserFromInternet(false) + return user.name } catch (rejectedReason) { - return rejectedReason; + return rejectedReason } } -gettingUserName(); +gettingUserName() /** - * Hiarukan kode di bawah ini - */ + * Hiarukan kode di bawah ini + */ -module.exports = { fetchingUserFromInternet, gettingUserName }; +module.exports = { fetchingUserFromInternet, gettingUserName } diff --git a/src/error-handling/main.js b/src/error-handling/main.js index 2650dba..2843747 100644 --- a/src/error-handling/main.js +++ b/src/error-handling/main.js @@ -49,46 +49,46 @@ // TODO 1 class ValidationError extends Error { - constructor(message) { - super(message); - this.name = 'ValidationError'; + constructor (message) { + super(message) + this.name = 'ValidationError' } } // TODO 2 -function validateNumberInput(a, b, c) { +function validateNumberInput (a, b, c) { if (typeof a !== 'number') { - throw new ValidationError('Argumen pertama harus number'); + throw new ValidationError('Argumen pertama harus number') } if (typeof b !== 'number') { - throw new ValidationError('Argumen kedua harus number'); + throw new ValidationError('Argumen kedua harus number') } if (typeof c !== 'number') { - throw new ValidationError('Argumen ketiga harus number'); + throw new ValidationError('Argumen ketiga harus number') } } const detectTriangle = (a, b, c) => { // TODO 3 try { - validateNumberInput(a, b, c); + validateNumberInput(a, b, c) if (a === b && b === c) { - return 'Segitiga sama sisi'; + return 'Segitiga sama sisi' } if (a === b || a === c || b === c) { - return 'Segitiga sama kaki'; + return 'Segitiga sama kaki' } - return 'Segitiga sembarang'; + return 'Segitiga sembarang' } catch (error) { - return error.message; + return error.message } -}; +} /** - * Hiraukan kode di bawah ini - */ -module.exports = { ValidationError, validateNumberInput, detectTriangle }; + * Hiraukan kode di bawah ini + */ +module.exports = { ValidationError, validateNumberInput, detectTriangle } diff --git a/src/function/main.js b/src/function/main.js index c6d326e..29c2cca 100644 --- a/src/function/main.js +++ b/src/function/main.js @@ -21,19 +21,19 @@ */ // TODO -function minimal(a, b) { +function minimal (a, b) { if (b < a) { - return b; + return b } - return a; + return a } -function power(a, b) { - return a ** b; +function power (a, b) { + return a ** b } /** * Hiraukan kode di bawah ini */ -module.exports = { minimal, power }; +module.exports = { minimal, power } diff --git a/src/functional-programming/main.js b/src/functional-programming/main.js index fd4d03d..c4fad51 100644 --- a/src/functional-programming/main.js +++ b/src/functional-programming/main.js @@ -16,17 +16,24 @@ const books = [ { title: 'The Ghost', author: 'Robert Harris', sales: 807311 }, { title: 'White Teeth', author: 'Zadie Smith', sales: 815586 }, { title: 'Fifty Shades of Grey', author: 'E. L. James', sales: 3758936 }, - { title: 'Jamie\'s Italy', author: 'Jamie Oliver', sales: 906968 }, + { title: "Jamie's Italy", author: 'Jamie Oliver', sales: 906968 }, { title: 'I Can Make You Thin', author: 'Paul McKenna', sales: 905086 }, - { title: 'Harry Potter and the Deathly Hallows', author: 'J.K Rowling', sales: 4475152 }, -]; + { + title: 'Harry Potter and the Deathly Hallows', + author: 'J.K Rowling', + sales: 4475152 + } +] // TODO -let greatAuthors = books.filter((book) => book.sales > 1000000); -greatAuthors = greatAuthors.map((book) => `${book.author} adalah penulis buku ${book.title} yang sangat hebat!`); +let greatAuthors = books.filter((book) => book.sales > 1000000) +greatAuthors = greatAuthors.map( + (book) => + `${book.author} adalah penulis buku ${book.title} yang sangat hebat!` +) /** * Hiraukan kode di bawah ini */ -module.exports = { books, greatAuthors }; +module.exports = { books, greatAuthors } diff --git a/src/logic-operator-and-if-else/main.js b/src/logic-operator-and-if-else/main.js index abfa2cb..7932b1a 100644 --- a/src/logic-operator-and-if-else/main.js +++ b/src/logic-operator-and-if-else/main.js @@ -18,27 +18,27 @@ * */ -function scoreChecker(score) { - let result; +function scoreChecker (score) { + let result // TODO if (score >= 90) { - result = 'Selamat! Anda mendapatkan nilai A.'; + result = 'Selamat! Anda mendapatkan nilai A.' } else if (score >= 80) { - result = 'Anda mendapatkan nilai B.'; + result = 'Anda mendapatkan nilai B.' } else if (score >= 70) { - result = 'Anda mendapatkan nilai C.'; + result = 'Anda mendapatkan nilai C.' } else if (score >= 60) { - result = 'Anda mendapatkan nilai D.'; + result = 'Anda mendapatkan nilai D.' } else { - result = 'Anda mendapatkan nilai E.'; + result = 'Anda mendapatkan nilai E.' } // Jangan hapus kode ini - return result; + return result } /** - * Hiraukan kode di bawah ini - */ -module.exports = scoreChecker; + * Hiraukan kode di bawah ini + */ +module.exports = scoreChecker diff --git a/src/map/main.js b/src/map/main.js index 006d98f..0aab989 100644 --- a/src/map/main.js +++ b/src/map/main.js @@ -13,14 +13,14 @@ const currency = new Map([ ['USD', 14000], ['JPY', 131], ['SGD', 11000], - ['MYR', 3500], -]); + ['MYR', 3500] +]) -const priceInJPY = 5000; -const priceInIDR = priceInJPY * currency.get('JPY'); +const priceInJPY = 5000 +const priceInIDR = priceInJPY * currency.get('JPY') // TODO /** - * Hiraukan kode di bawah ini - */ -module.exports = { currency, priceInJPY, priceInIDR }; + * Hiraukan kode di bawah ini + */ +module.exports = { currency, priceInJPY, priceInIDR } diff --git a/src/module/Tiger.js b/src/module/Tiger.js index 55cde0e..99d3018 100644 --- a/src/module/Tiger.js +++ b/src/module/Tiger.js @@ -1,14 +1,14 @@ /* eslint-disable class-methods-use-this */ class Tiger { - constructor() { - this.strength = Math.floor(Math.random() * 100); + constructor () { + this.strength = Math.floor(Math.random() * 100) } - growl() { - return 'grrrrrrr'; + growl () { + return 'grrrrrrr' } } // TODO: 1 -module.exports = Tiger; +module.exports = Tiger diff --git a/src/module/Wolf.js b/src/module/Wolf.js index 6c9e7de..24fd88d 100644 --- a/src/module/Wolf.js +++ b/src/module/Wolf.js @@ -1,13 +1,13 @@ /* eslint-disable class-methods-use-this */ class Wolf { - constructor() { - this.strength = Math.floor(Math.random() * 100); + constructor () { + this.strength = Math.floor(Math.random() * 100) } - howl() { - return 'Auuuuuuuuu'; + howl () { + return 'Auuuuuuuuu' } } // TODO 2 -module.exports = Wolf; +module.exports = Wolf diff --git a/src/module/main.js b/src/module/main.js index 7e9eb35..5a0f176 100644 --- a/src/module/main.js +++ b/src/module/main.js @@ -14,25 +14,28 @@ */ // TODO 3 -const Tiger = require('./Tiger'); -const Wolf = require('./Wolf'); +const Tiger = require('./Tiger') +const Wolf = require('./Wolf') const fight = (tiger, wolf) => { if (tiger.strength > wolf.strength) { - return tiger.growl(); + return tiger.growl() } if (wolf.strength > tiger.strength) { - return wolf.howl(); + return wolf.howl() } - return 'Harimau dan serigala sama-sama kuat!'; -}; + return 'Harimau dan serigala sama-sama kuat!' +} -const myTiger = new Tiger(); -const myWolf = new Wolf(); +const myTiger = new Tiger() +const myWolf = new Wolf() -const result = fight(myTiger, myWolf); +const result = fight(myTiger, myWolf) // TODO 4 module.exports = { - fight, myTiger, myWolf, result, -}; + fight, + myTiger, + myWolf, + result +} diff --git a/src/object/main.js b/src/object/main.js index 4bd4bbb..edc65f9 100644 --- a/src/object/main.js +++ b/src/object/main.js @@ -29,13 +29,13 @@ const restaurant = { city: 'Palembang', 'favorite drink': 'Orange juice', 'favorite food': 'Rendang', - isVegan: false, -}; + isVegan: false +} -const { name } = restaurant; -const favoriteDrink = restaurant['favorite drink']; +const { name } = restaurant +const favoriteDrink = restaurant['favorite drink'] /** * Hiraukan kode di bawah ini */ -module.exports = { restaurant, name, favoriteDrink }; +module.exports = { restaurant, name, favoriteDrink } diff --git a/src/oop/main.js b/src/oop/main.js index 2a56ef6..1aa404d 100644 --- a/src/oop/main.js +++ b/src/oop/main.js @@ -30,40 +30,44 @@ // TODO class Animal { - constructor(name, age, isMammal) { - this.name = name; - this.age = age; - this.isMammal = isMammal; + constructor (name, age, isMammal) { + this.name = name + this.age = age + this.isMammal = isMammal } } class Rabbit extends Animal { - constructor(name, age) { - super(name, age, true); + constructor (name, age) { + super(name, age, true) } - eat() { - return `${this.name} sedang makan!`; + eat () { + return `${this.name} sedang makan!` } } class Eagle extends Animal { - constructor(name, age) { - super(name, age, false); + constructor (name, age) { + super(name, age, false) } - fly() { - return `${this.name} sedang terbang!`; + fly () { + return `${this.name} sedang terbang!` } } -const myRabbit = new Rabbit('Labi', 2); -const myEagle = new Eagle('Elo', 4); +const myRabbit = new Rabbit('Labi', 2) +const myEagle = new Eagle('Elo', 4) /** * Hiraukan kode di bawah ini */ module.exports = { - Animal, Rabbit, Eagle, myRabbit, myEagle, -}; + Animal, + Rabbit, + Eagle, + myRabbit, + myEagle +} diff --git a/src/variable-and-data-type/main.js b/src/variable-and-data-type/main.js index 7999d27..79c6c27 100644 --- a/src/variable-and-data-type/main.js +++ b/src/variable-and-data-type/main.js @@ -8,14 +8,17 @@ */ // TODO -const firstName = 'Mgs.'; -const lastName = 'Tabrani'; -const age = 19; -const isMarried = false; +const firstName = 'Mgs.' +const lastName = 'Tabrani' +const age = 19 +const isMarried = false /** * Hiraukan kode di bawah ini */ module.exports = { - firstName, lastName, age, isMarried, -}; + firstName, + lastName, + age, + isMarried +} diff --git a/test/array.test.js b/test/array.test.js index 8b588d2..600c0d1 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -1,13 +1,11 @@ /* eslint-disable no-undef */ -const evenNumber = require('../src/array/main'); +const evenNumber = require('../src/array/main') test('array should contain even number from 2 till 100', () => { const arrayTest = [ - 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, - 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, - 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, - 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, - 90, 92, 94, 96, 98, 100, - ]; - expect(evenNumber).toStrictEqual(arrayTest); -}); + 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, + 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, + 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100 + ] + expect(evenNumber).toStrictEqual(arrayTest) +}) diff --git a/test/concurrency.test.js b/test/concurrency.test.js index c221ad0..efff84b 100644 --- a/test/concurrency.test.js +++ b/test/concurrency.test.js @@ -1,7 +1,7 @@ /* eslint-disable no-undef */ -const NetworkError = require('../src/concurrency/NetworkError'); +const NetworkError = require('../src/concurrency/NetworkError') test('NetworkError instance should has correct name member', () => { - const newtworkErrorInstance = new NetworkError(); - expect(newtworkErrorInstance.name).toStrictEqual('NetworkError'); -}); + const newtworkErrorInstance = new NetworkError() + expect(newtworkErrorInstance.name).toStrictEqual('NetworkError') +}) diff --git a/test/error-handling.test.js b/test/error-handling.test.js index b93e001..ce57584 100644 --- a/test/error-handling.test.js +++ b/test/error-handling.test.js @@ -1,8 +1,11 @@ /* eslint-disable no-undef */ /* eslint-disable import/no-extraneous-dependencies */ -const { expect } = require('@jest/globals'); -const { ValidationError, validateNumberInput } = require('../src/error-handling/main'); +const { expect } = require('@jest/globals') +const { + ValidationError, + validateNumberInput +} = require('../src/error-handling/main') test('validateNumberInput should throw the right one', () => { - expect(validateNumberInput).toThrowError(ValidationError); -}); + expect(validateNumberInput).toThrowError(ValidationError) +}) diff --git a/test/function.test.js b/test/function.test.js index 68c7c9d..c398388 100644 --- a/test/function.test.js +++ b/test/function.test.js @@ -1,12 +1,12 @@ /* eslint-disable no-undef */ /* eslint-disable import/no-extraneous-dependencies */ -const { expect } = require('@jest/globals'); -const { minimal, power } = require('../src/function/main'); +const { expect } = require('@jest/globals') +const { minimal, power } = require('../src/function/main') test('minimal function should get the minimum value', () => { - expect(minimal(1, 2)).toBe(1); -}); + expect(minimal(1, 2)).toBe(1) +}) test('power function should get the power operation', () => { - expect(power(3, 2)).toBe(9); -}); + expect(power(3, 2)).toBe(9) +}) diff --git a/test/functional-programming.test.js b/test/functional-programming.test.js index 567d9ce..b243844 100644 --- a/test/functional-programming.test.js +++ b/test/functional-programming.test.js @@ -1,14 +1,14 @@ /* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable no-undef */ -const { expect } = require('@jest/globals'); -const { greatAuthors } = require('../src/functional-programming/main'); +const { expect } = require('@jest/globals') +const { greatAuthors } = require('../src/functional-programming/main') test('the variable should contain the right value', () => { const greatAuthorsTest = [ 'Dan Brown adalah penulis buku The Da Vinci Code yang sangat hebat!', 'E. L. James adalah penulis buku Fifty Shades of Grey yang sangat hebat!', - 'J.K Rowling adalah penulis buku Harry Potter and the Deathly Hallows yang sangat hebat!', - ]; + 'J.K Rowling adalah penulis buku Harry Potter and the Deathly Hallows yang sangat hebat!' + ] - expect(greatAuthors).toStrictEqual(greatAuthorsTest); -}); + expect(greatAuthors).toStrictEqual(greatAuthorsTest) +}) diff --git a/test/logic-operator-and-if-else.test.js b/test/logic-operator-and-if-else.test.js index 179b830..657d673 100644 --- a/test/logic-operator-and-if-else.test.js +++ b/test/logic-operator-and-if-else.test.js @@ -1,6 +1,6 @@ /* eslint-disable no-undef */ -const scoreChecker = require('../src/logic-operator-and-if-else/main'); +const scoreChecker = require('../src/logic-operator-and-if-else/main') test('scoreChecker function should get result based on score', () => { - expect(scoreChecker(90)).toBe('Selamat! Anda mendapatkan nilai A.'); -}); + expect(scoreChecker(90)).toBe('Selamat! Anda mendapatkan nilai A.') +}) diff --git a/test/map.test.js b/test/map.test.js index dc955af..341762f 100644 --- a/test/map.test.js +++ b/test/map.test.js @@ -1,16 +1,16 @@ /* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable no-undef */ -const { expect } = require('@jest/globals'); -const { currency, priceInJPY, priceInIDR } = require('../src/map/main'); +const { expect } = require('@jest/globals') +const { currency, priceInJPY, priceInIDR } = require('../src/map/main') test('the variables should contain exact values', () => { const testCurrency = new Map([ ['USD', 14000], ['JPY', 131], ['SGD', 11000], - ['MYR', 3500], - ]); - expect(currency).toStrictEqual(testCurrency); - expect(priceInJPY).toStrictEqual(5000); - expect(priceInIDR).toStrictEqual(131 * priceInJPY); -}); + ['MYR', 3500] + ]) + expect(currency).toStrictEqual(testCurrency) + expect(priceInJPY).toStrictEqual(5000) + expect(priceInIDR).toStrictEqual(131 * priceInJPY) +}) diff --git a/test/module.test.js b/test/module.test.js index af89774..7068df8 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -1,12 +1,12 @@ /* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable no-undef */ -const { expect } = require('@jest/globals'); -const { myTiger, myWolf } = require('../src/module/main'); +const { expect } = require('@jest/globals') +const { myTiger, myWolf } = require('../src/module/main') test('myTiger should has the right method', () => { - expect(myTiger.growl()).toBe('grrrrrrr'); -}); + expect(myTiger.growl()).toBe('grrrrrrr') +}) test('myWolf should has the right method', () => { - expect(myWolf.howl()).toBe('Auuuuuuuuu'); -}); + expect(myWolf.howl()).toBe('Auuuuuuuuu') +}) diff --git a/test/object.test.js b/test/object.test.js index 98342c3..0fc7c70 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -1,12 +1,12 @@ /* eslint-disable no-undef */ /* eslint-disable import/no-extraneous-dependencies */ -const { expect } = require('@jest/globals'); -const { restaurant, name, favoriteDrink } = require('../src/object/main'); +const { expect } = require('@jest/globals') +const { restaurant, name, favoriteDrink } = require('../src/object/main') test('name variable should contain name value in restaurant', () => { - expect(name).toStrictEqual(restaurant.name); -}); + expect(name).toStrictEqual(restaurant.name) +}) test('favoriteDrink variable should contain favorite drink value in restaurant', () => { - expect(favoriteDrink).toStrictEqual(restaurant['favorite drink']); -}); + expect(favoriteDrink).toStrictEqual(restaurant['favorite drink']) +}) diff --git a/test/oop.test.js b/test/oop.test.js index 3618627..accd7d2 100644 --- a/test/oop.test.js +++ b/test/oop.test.js @@ -1,11 +1,11 @@ /* eslint-disable import/no-extraneous-dependencies */ -const { test, expect } = require('@jest/globals'); -const { myRabbit, myEagle } = require('../src/oop/main'); +const { test, expect } = require('@jest/globals') +const { myRabbit, myEagle } = require('../src/oop/main') test('myRabbit instance should has the right method', () => { - expect(myRabbit.eat()).toBe('Labi sedang makan!'); -}); + expect(myRabbit.eat()).toBe('Labi sedang makan!') +}) test('myEagle instance should has the right method', () => { - expect(myEagle.fly()).toBe('Elo sedang terbang!'); -}); + expect(myEagle.fly()).toBe('Elo sedang terbang!') +}) diff --git a/test/variable-and-data-type.test.js b/test/variable-and-data-type.test.js index c179503..b53dbed 100644 --- a/test/variable-and-data-type.test.js +++ b/test/variable-and-data-type.test.js @@ -1,13 +1,16 @@ /* eslint-disable no-undef */ /* eslint-disable import/no-extraneous-dependencies */ -const { expect } = require('@jest/globals'); +const { expect } = require('@jest/globals') const { - firstName, lastName, age, isMarried, -} = require('../src/variable-and-data-type/main'); + firstName, + lastName, + age, + isMarried +} = require('../src/variable-and-data-type/main') test('the variables should contain appropriate values', () => { - expect(firstName).toStrictEqual('Mgs.'); - expect(lastName).toStrictEqual('Tabrani'); - expect(age).toStrictEqual(19); - expect(isMarried).toStrictEqual(false); -}); + expect(firstName).toStrictEqual('Mgs.') + expect(lastName).toStrictEqual('Tabrani') + expect(age).toStrictEqual(19) + expect(isMarried).toStrictEqual(false) +})