diff --git a/assets/array-methods-exercises.js b/assets/array-methods-exercises.js new file mode 100644 index 000000000..29e0adc9a --- /dev/null +++ b/assets/array-methods-exercises.js @@ -0,0 +1,22 @@ +const flowers = [ + { name: 'tulip', color: 'red' }, + { name: 'dandelion', color: 'yellow' }, + { name: 'rose', color: 'red' }, + { name: 'daisy', color: 'white' }, + { name: 'lily', color: 'white' }, +]; + +// Exercise 1 +// use `map` to make the first letter of al flowers a capital. +// so 'tulip' becomes 'Tulip' etc. + +// Exercise 2 +// use `filter` to get only the flowers with a name of 4 characters + +// Exercise 3 +// use `reduce` to create an object that contains +// the total number of flowers for each color + +// Exercise 4 (bonus) +// use `some` or `every` in combination with another array method +// to find out if all flowers with five letters are red diff --git a/assets/sync-async.js b/assets/sync-async.js new file mode 100644 index 000000000..8ccda654d --- /dev/null +++ b/assets/sync-async.js @@ -0,0 +1,80 @@ +const greenGrocer = { + buyApples: function(quantity) { + return quantity + ' apples'; + }, + buyTomatoes: function(quantity) { + return quantity + ' tomatoes'; + }, + buyCauliflower: function(quantity) { + return quantity + ' cauliflower'; + }, +}; + +const cheeseShop = { + buyCheese: function(type, weight) { + this.cutCheese(type, weight); + + return weight + 'gr ' + type + ' cheese'; + }, + + cutCheese: function(type, weight) { + const stopTime = Date.now() + 100; + while (Date.now() < stopTime); + }, +}; + +const bakery = { + buyBread: function(breadReadyCallback, type, quantity) { + setTimeout(function() { + breadReadyCallback(quantity + ' ' + type); + }, 1000); + }, +}; + +function visitGreengrocer() { + console.log('=== Greengrocer ==='); + + const beforeBuy = Date.now(); + + const fruitsAndVegetables = [ + greenGrocer.buyApples(2), + greenGrocer.buyTomatoes(2), + greenGrocer.buyCauliflower(1), + ]; + + const timeTaken = (Date.now() - beforeBuy).toFixed(2); + + console.log('You bought: ', fruitsAndVegetables); + console.log('This took: ' + timeTaken + 'ms\n'); +} + +function visitCheeseShop() { + console.log('=== Cheese Shop ==='); + const beforeBuy = Date.now(); + + const cheese = cheeseShop.buyCheese('belegen', 500); + + const timeTaken = (Date.now() - beforeBuy).toFixed(2); + + console.log('You bought: ' + cheese + ''); + console.log('This took: ' + timeTaken + 'ms\n'); +} + +function visitBakery() { + console.log('=== Bakery ==='); + const beforeBuy = Date.now(); + + const breadReady = function(bread) { + const timeTaken = (Date.now() - beforeBuy).toFixed(2); + console.log('--- Callback from Bakery ---'); + console.log('You bought: ' + bread + ''); + console.log('This took: ' + timeTaken + 'ms\n'); + }; + + bakery.buyBread(breadReady, 'fijn volkoren', 1); + console.log(`We'll call you back!\n`); +} + +visitGreengrocer(); // ?. +visitBakery(); // ?. +visitCheeseShop(); // ?.