├── add-multiply.js ├── animal-count.js ├── cheapest-phone.js ├── divisible-by.js ├── duplicate.js ├── input-error.js ├── recap.js ├── reverse.js └── total-price.js /add-multiply.js: -------------------------------------------------------------------------------- 1 | /* 2 | chairWood = 3cft/chair 3 | tableWood = 10cft/table 4 | bedWood = 50cft/bed 5 | */ 6 | 7 | function woodCalculator(chairQuantity, tableQuantity, bedQuantity) { 8 | const perChairWood = 3; 9 | const perTableWood = 10; 10 | const perBedWood = 50; 11 | // wood calculation 12 | const chairWoodQuantity = chairQuantity * perChairWood; 13 | const tableWoodQuantity = tableQuantity * perTableWood; 14 | const bedWoodQuantity = bedQuantity * perBedWood; 15 | // adding all wood quantity 16 | const totalWood = chairWoodQuantity + tableWoodQuantity + bedWoodQuantity; 17 | return totalWood; 18 | } 19 | 20 | const firstOption = woodCalculator(1, 2, 3); 21 | console.log(firstOption); -------------------------------------------------------------------------------- /animal-count.js: -------------------------------------------------------------------------------- 1 | function animalCount(miles) { 2 | const animalDensityFirst10Miles = 10; 3 | const animalDensitySecond10Miles = 50; 4 | const animalDensityRestMiles = 100; 5 | if (miles <= 10) { 6 | const count = miles * animalDensityFirst10Miles; 7 | return count; 8 | } 9 | else if (miles <= 20) { 10 | const firstDenseAnimals = 10 * animalDensityFirst10Miles; 11 | const restMiles = miles - 10; 12 | const secondDenseAnimals = restMiles * animalDensitySecond10Miles; 13 | const totalAnimals = firstDenseAnimals + secondDenseAnimals; 14 | return totalAnimals; 15 | } 16 | else { 17 | const firstDenseAnimals = 10 * animalDensityFirst10Miles; 18 | const secondDenseAnimals = 10 * animalDensitySecond10Miles; 19 | const restMiles = miles - 20; 20 | const RestDenseAnimals = restMiles * animalDensityRestMiles; 21 | const totalAnimals = firstDenseAnimals + secondDenseAnimals + RestDenseAnimals; 22 | return totalAnimals; 23 | } 24 | } 25 | 26 | const animals = animalCount(35); 27 | console.log(animals); -------------------------------------------------------------------------------- /cheapest-phone.js: -------------------------------------------------------------------------------- 1 | const phones = [ 2 | { name: 'samsung s5', price: 45000, camera: 10, storage: 32 }, 3 | { name: 'walton m32', price: 15000, camera: 8, storage: 8 }, 4 | { name: 'shaomi m3', price: 12000, camera: 8, storage: 16 }, 5 | { name: 'oppo a2', price: 17000, camera: 8, storage: 32 }, 6 | { name: 'nokia n95', price: 8000, camera: 8, storage: 4 }, 7 | { name: 'htc h81', price: 25000, camera: 8, storage: 16 } 8 | ]; 9 | 10 | let cheapest = phones[0]; 11 | for (const phone of phones) { 12 | // compare price only 13 | if (phone.price < cheapest.price) { 14 | cheapest = phone; 15 | } 16 | } 17 | console.log(cheapest); -------------------------------------------------------------------------------- /divisible-by.js: -------------------------------------------------------------------------------- 1 | for (let i = 1; i <= 50; i++) { 2 | if (i % 3 == 0 && i % 5 == 0) { 3 | console.log('foobar'); 4 | } 5 | else if (i % 3 == 0) { 6 | console.log('foo'); 7 | } 8 | else if (i % 5 == 0) { 9 | console.log('bar'); 10 | } 11 | else { 12 | console.log(i); 13 | } 14 | } -------------------------------------------------------------------------------- /duplicate.js: -------------------------------------------------------------------------------- 1 | const names = ['abul', 'babul', 'cabul', 'dabul', 'ebul', 'fabul', 'babul', 'ebul', 'babul', 'gabul', 'abul', 'habul', 'dabul']; 2 | 3 | function removeDuplicate(names) { 4 | const unique = []; 5 | // for (let i = 0; i < names.length; i++) { 6 | // const element = names[i]; 7 | // // console.log(element); 8 | // } 9 | for (const element of names) { 10 | if (unique.indexOf(element) == -1) { 11 | unique.push(element); 12 | } 13 | } 14 | return unique; 15 | } 16 | 17 | const uniqueNames = removeDuplicate(names); 18 | console.log(uniqueNames); -------------------------------------------------------------------------------- /input-error.js: -------------------------------------------------------------------------------- 1 | function add(num1, num2) { 2 | const sum = num1 + num2; 3 | return sum; 4 | } 5 | 6 | const firstTotal = add(63, 67); 7 | // console.log(firstTotal); 8 | const secondTotal = add(54, 981); 9 | // console.log(secondTotal); 10 | const thirdTotal = add(12, 14); 11 | console.log(thirdTotal); 12 | 13 | function multiply(num1, num2) { 14 | console.log('parameters', num1, num2); 15 | const result = num1 * num2; 16 | return result; 17 | } 18 | 19 | const firstResult = multiply(13, 12); 20 | console.log(firstResult); -------------------------------------------------------------------------------- /recap.js: -------------------------------------------------------------------------------- 1 | function largestElement(numbers) { 2 | let max = numbers[0]; // 3 | for (let i = 0; i < numbers.length; i++) { 4 | const element = numbers[i]; 5 | if (element > max) { 6 | max = element; 7 | } 8 | } 9 | } 10 | 11 | function smallestElement(numbers) { 12 | let min = numbers[0]; // 13 | for (let i = 0; i < numbers.length; i++) { 14 | const element = numbers[i]; 15 | if (element < min) { 16 | min = element; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /reverse.js: -------------------------------------------------------------------------------- 1 | const greetings = 'Hello, how are you?'; 2 | 3 | function reverseString(text) { 4 | let reverse = ''; 5 | for (const letter of text) { 6 | reverse = letter + reverse; 7 | } 8 | } 9 | const reversed = reverseString(greetings); 10 | console.log(reversed); -------------------------------------------------------------------------------- /total-price.js: -------------------------------------------------------------------------------- 1 | const products = [ 2 | { name: 'laptop', price: 43000 }, 3 | { name: 'shirt', price: 500 }, 4 | { name: 'watch', price: 3680 }, 5 | { name: 'phone', price: 55000 } 6 | ]; 7 | 8 | let totalPrice = 0; 9 | 10 | for (const product of products) { 11 | totalPrice = totalPrice + product.price; 12 | } 13 | // console.log(totalPrice); 14 | 15 | const cart = [ 16 | { name: 'laptop', price: 43000, quantity: 1 }, 17 | { name: 'shirt', price: 500, quantity: 8 }, 18 | { name: 'watch', price: 3680, quantity: 3 }, 19 | { name: 'phone', price: 55000, quantity: 1 } 20 | ]; 21 | let cartTotal = 0; 22 | for (const product of cart) { 23 | console.log(product); 24 | const productTotal = product.price * product.quantity; 25 | cartTotal = cartTotal + productTotal; 26 | } 27 | console.log(cartTotal); --------------------------------------------------------------------------------