From d6bc65be802c393117d56db99a11123dead7abb6 Mon Sep 17 00:00:00 2001 From: Ghufran Date: Wed, 27 Mar 2019 21:14:05 +0100 Subject: [PATCH 1/9] Some more map, filter and =>, a program that doubles the odd numbers in an array and throws away the even numbers and another program using map, filter & reduce. --- week2.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 week2.js diff --git a/week2.js b/week2.js new file mode 100644 index 000000000..e69de29bb From 1fbde9db7ac8aa5b63af8351197f88c763c59f6b Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Wed, 27 Mar 2019 21:20:26 +0100 Subject: [PATCH 2/9] Update and rename week2.js to ghufran/week2.js --- ghufran/week2.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ week2.js | 0 2 files changed, 88 insertions(+) create mode 100644 ghufran/week2.js delete mode 100644 week2.js diff --git a/ghufran/week2.js b/ghufran/week2.js new file mode 100644 index 000000000..4bc6f36ab --- /dev/null +++ b/ghufran/week2.js @@ -0,0 +1,88 @@ +// ----------map & filter ------------ +const myNumbers = [1, 2, 3, 4]; + +// double all the elements using map +const newNumbers = myNumbers.map(elem => elem * 2); + +console.log(newNumbers); // (4)[2, 4, 6, 8] + +// choose the odd numbers using filter +const numbers = myNumbers.filter(elem => elem % 2 !== 0); +const doubleOddNumbers = numbers.map(elem => elem * 2); + +console.log(doubleOddNumbers); // (2)[2, 6] +console.log(doubleOddNumbers(myNumbers)); + +// or choose the odd numbers using filter this way + +// const numbers = myNumbers.filter(elem => elem % 2 !== 0); +// const doubleOddNumbers = numbers.map(elem => elem * 2); + +console.log(doubleOddNumbers); // (2)[2, 6] +console.log(doubleOddNumbers(myNumbers)); + +// ---------------------------- +const monday = [ + { + name: 'Write a summary HTML/CSS', + duration: 180, + }, + { + name: 'Some web development', + duration: 120, + }, + { + name: 'Fix homework for class10', + duration: 20, + }, + { + name: 'Talk to a lot of people', + duration: 200, + }, +]; + +const tuesday = [ + { + name: 'Keep writing summary', + duration: 240, + }, + { + name: 'Some more web development', + duration: 180, + }, + { + name: 'Staring out the window', + duration: 10, + }, + { + name: 'Talk to a lot of people', + duration: 200, + }, + { + name: 'Look at application assignments new students', + duration: 40, + }, +]; + +// ---------- +/* cSpell: disable */ +const maartjesTasks = monday.concat(tuesday); +const maartjesHourlyRate = 20; + +// Map the tasks to durations in hours +const allTasks = maartjesTasks.map(elem => ({ ...elem, duration: elem.duration / 60 })); + +// Filter out everything that took less than two hours +const filteredTasks = allTasks.filter(elem => elem.duration >= 2); + +// Multiply the each duration by a per-hour rate for billing (use €20/hour) and sum it all up. +const sumEarning = filteredTasks.reduce((acc, elem) => { + const earning = elem.duration * maartjesHourlyRate; + return acc + earning; +}, 0); +/* cSpell: enable */ +console.log(sumEarning); + +// Output a formatted Euro amount, rounded to Euro cents +const roundEuros = sumEarning.toFixed(2); +console.log(roundEuros); diff --git a/week2.js b/week2.js deleted file mode 100644 index e69de29bb..000000000 From 7ff0b750e61bd4bd5db442a8df1c70cac1a0ea7e Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Thu, 28 Mar 2019 13:58:17 +0100 Subject: [PATCH 3/9] Update week2.js --- ghufran/week2.js | 68 ++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/ghufran/week2.js b/ghufran/week2.js index 4bc6f36ab..39ed5d33b 100644 --- a/ghufran/week2.js +++ b/ghufran/week2.js @@ -1,27 +1,13 @@ -// ----------map & filter ------------ -const myNumbers = [1, 2, 3, 4]; - -// double all the elements using map -const newNumbers = myNumbers.map(elem => elem * 2); - -console.log(newNumbers); // (4)[2, 4, 6, 8] +function doubleOddNumbers(numbers) { + const newNumbers = numbers.filter(number => number % 2 !== 0); + const doubleNumbers = newNumbers.map(number => number * 2); + return doubleNumbers; +} -// choose the odd numbers using filter -const numbers = myNumbers.filter(elem => elem % 2 !== 0); -const doubleOddNumbers = numbers.map(elem => elem * 2); - -console.log(doubleOddNumbers); // (2)[2, 6] -console.log(doubleOddNumbers(myNumbers)); - -// or choose the odd numbers using filter this way - -// const numbers = myNumbers.filter(elem => elem % 2 !== 0); -// const doubleOddNumbers = numbers.map(elem => elem * 2); - -console.log(doubleOddNumbers); // (2)[2, 6] +const myNumbers = [1, 2, 3, 4]; console.log(doubleOddNumbers(myNumbers)); - // ---------------------------- + const monday = [ { name: 'Write a summary HTML/CSS', @@ -63,26 +49,34 @@ const tuesday = [ duration: 40, }, ]; - -// ---------- /* cSpell: disable */ const maartjesTasks = monday.concat(tuesday); const maartjesHourlyRate = 20; -// Map the tasks to durations in hours -const allTasks = maartjesTasks.map(elem => ({ ...elem, duration: elem.duration / 60 })); +function computeEarnings(tasks, hourlyRate) { + const duration = tasks.map(task => { + const hours = task.duration / 60; + return { ...task, duration: hours }; + }); -// Filter out everything that took less than two hours -const filteredTasks = allTasks.filter(elem => elem.duration >= 2); + const filteredHour = duration.filter(elem => elem.duration >= 2); -// Multiply the each duration by a per-hour rate for billing (use €20/hour) and sum it all up. -const sumEarning = filteredTasks.reduce((acc, elem) => { - const earning = elem.duration * maartjesHourlyRate; - return acc + earning; -}, 0); -/* cSpell: enable */ -console.log(sumEarning); + return filteredHour.reduce((acc, elem) => { + acc += elem.duration * hourlyRate; + return acc; + }, 0); +} +console.log(computeEarnings(maartjesTasks, maartjesHourlyRate)); -// Output a formatted Euro amount, rounded to Euro cents -const roundEuros = sumEarning.toFixed(2); -console.log(roundEuros); +const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); + +const roundEuros = earnings.toFixed(2); + +console.log(`Maartje has earned €${roundEuros}`); + +module.exports = { + maartjesTasks, + maartjesHourlyRate, + computeEarnings, +}; +/* cSpell: enable */ From 433d0d7a0c00d0adb38056d6e0aa4fa9168eee29 Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Thu, 28 Mar 2019 14:11:05 +0100 Subject: [PATCH 4/9] Update week2.js --- ghufran/week2.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ghufran/week2.js b/ghufran/week2.js index 39ed5d33b..59de5370a 100644 --- a/ghufran/week2.js +++ b/ghufran/week2.js @@ -1,11 +1,11 @@ function doubleOddNumbers(numbers) { - const newNumbers = numbers.filter(number => number % 2 !== 0); - const doubleNumbers = newNumbers.map(number => number * 2); + const newNumbers = numbers.filter(number => number % 2 !== 0); // filter the odd nrs + const doubleNumbers = newNumbers.map(number => number * 2); // double the odd nrs return doubleNumbers; } const myNumbers = [1, 2, 3, 4]; -console.log(doubleOddNumbers(myNumbers)); +console.log(doubleOddNumbers(myNumbers)); // [2, 6] the double of [1,3] // ---------------------------- const monday = [ @@ -55,12 +55,14 @@ const maartjesHourlyRate = 20; function computeEarnings(tasks, hourlyRate) { const duration = tasks.map(task => { + // turn mins to hours const hours = task.duration / 60; return { ...task, duration: hours }; }); + // remove less than 2 hours const filteredHour = duration.filter(elem => elem.duration >= 2); - + // earnings for the duration less than 2 hours return filteredHour.reduce((acc, elem) => { acc += elem.duration * hourlyRate; return acc; @@ -70,9 +72,11 @@ console.log(computeEarnings(maartjesTasks, maartjesHourlyRate)); const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); -const roundEuros = earnings.toFixed(2); +// set the decimal nrs to have only two values +const roundEuros = earnings.toFixed(2); // 373.33333333333337 -console.log(`Maartje has earned €${roundEuros}`); +// call the value of toFixed(2) +console.log(`Maartje has earned €${roundEuros}`); // Maartje has earned €373.33 module.exports = { maartjesTasks, From 3412b5707ae9e60a611e439a2cb75e52c69d7013 Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Thu, 28 Mar 2019 14:47:25 +0100 Subject: [PATCH 5/9] Update and rename week2.js to map-filter.js --- ghufran/map-filter.js | 11 ++++++ ghufran/week2.js | 86 ------------------------------------------- 2 files changed, 11 insertions(+), 86 deletions(-) create mode 100644 ghufran/map-filter.js delete mode 100644 ghufran/week2.js diff --git a/ghufran/map-filter.js b/ghufran/map-filter.js new file mode 100644 index 000000000..084bc0f98 --- /dev/null +++ b/ghufran/map-filter.js @@ -0,0 +1,11 @@ +'use strict'; + +function doubleOddNumbers(numbers) { + const newNumbers = numbers.filter(number => number % 2 !== 0); // filter the odd nrs + const doubleNumbers = newNumbers.map(number => number * 2); // double the odd nrs + return doubleNumbers; +} + +const myNumbers = [1, 2, 3, 4]; +console.log(doubleOddNumbers(myNumbers)); // [2, 6] the double of [1,3] + diff --git a/ghufran/week2.js b/ghufran/week2.js deleted file mode 100644 index 59de5370a..000000000 --- a/ghufran/week2.js +++ /dev/null @@ -1,86 +0,0 @@ -function doubleOddNumbers(numbers) { - const newNumbers = numbers.filter(number => number % 2 !== 0); // filter the odd nrs - const doubleNumbers = newNumbers.map(number => number * 2); // double the odd nrs - return doubleNumbers; -} - -const myNumbers = [1, 2, 3, 4]; -console.log(doubleOddNumbers(myNumbers)); // [2, 6] the double of [1,3] -// ---------------------------- - -const monday = [ - { - name: 'Write a summary HTML/CSS', - duration: 180, - }, - { - name: 'Some web development', - duration: 120, - }, - { - name: 'Fix homework for class10', - duration: 20, - }, - { - name: 'Talk to a lot of people', - duration: 200, - }, -]; - -const tuesday = [ - { - name: 'Keep writing summary', - duration: 240, - }, - { - name: 'Some more web development', - duration: 180, - }, - { - name: 'Staring out the window', - duration: 10, - }, - { - name: 'Talk to a lot of people', - duration: 200, - }, - { - name: 'Look at application assignments new students', - duration: 40, - }, -]; -/* cSpell: disable */ -const maartjesTasks = monday.concat(tuesday); -const maartjesHourlyRate = 20; - -function computeEarnings(tasks, hourlyRate) { - const duration = tasks.map(task => { - // turn mins to hours - const hours = task.duration / 60; - return { ...task, duration: hours }; - }); - - // remove less than 2 hours - const filteredHour = duration.filter(elem => elem.duration >= 2); - // earnings for the duration less than 2 hours - return filteredHour.reduce((acc, elem) => { - acc += elem.duration * hourlyRate; - return acc; - }, 0); -} -console.log(computeEarnings(maartjesTasks, maartjesHourlyRate)); - -const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); - -// set the decimal nrs to have only two values -const roundEuros = earnings.toFixed(2); // 373.33333333333337 - -// call the value of toFixed(2) -console.log(`Maartje has earned €${roundEuros}`); // Maartje has earned €373.33 - -module.exports = { - maartjesTasks, - maartjesHourlyRate, - computeEarnings, -}; -/* cSpell: enable */ From b1448be092c890b1b36ec656c3da9a846d78b898 Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Thu, 28 Mar 2019 14:48:25 +0100 Subject: [PATCH 6/9] Create maartjes-work.js --- ghufran/maartjes-work.js | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ghufran/maartjes-work.js diff --git a/ghufran/maartjes-work.js b/ghufran/maartjes-work.js new file mode 100644 index 000000000..e420a496b --- /dev/null +++ b/ghufran/maartjes-work.js @@ -0,0 +1,78 @@ +'use strict'; + +const monday = [ + { + name: 'Write a summary HTML/CSS', + duration: 180, + }, + { + name: 'Some web development', + duration: 120, + }, + { + name: 'Fix homework for class10', + duration: 20, + }, + { + name: 'Talk to a lot of people', + duration: 200, + }, +]; + +const tuesday = [ + { + name: 'Keep writing summary', + duration: 240, + }, + { + name: 'Some more web development', + duration: 180, + }, + { + name: 'Staring out the window', + duration: 10, + }, + { + name: 'Talk to a lot of people', + duration: 200, + }, + { + name: 'Look at application assignments new students', + duration: 40, + }, +]; +/* cSpell: disable */ +const maartjesTasks = monday.concat(tuesday); +const maartjesHourlyRate = 20; + +function computeEarnings(tasks, hourlyRate) { + const duration = tasks.map(task => { + // turn mins to hours + const hours = task.duration / 60; + return { ...task, duration: hours }; + }); + + // remove less than 2 hours + const filteredHour = duration.filter(elem => elem.duration >= 2); + // earnings for the duration less than 2 hours + return filteredHour.reduce((acc, elem) => { + acc += elem.duration * hourlyRate; + return acc; + }, 0); +} +console.log(computeEarnings(maartjesTasks, maartjesHourlyRate)); + +const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); + +// set the decimal nrs to have only two values +const roundEuros = earnings.toFixed(2); // 373.33333333333337 + +// call the value of toFixed(2) +console.log(`Maartje has earned €${roundEuros}`); // Maartje has earned €373.33 + +module.exports = { + maartjesTasks, + maartjesHourlyRate, + computeEarnings, +}; +/* cSpell: enable */ From f383e8ee6212ba1f722c3fdc167e5da68f054536 Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Sat, 30 Mar 2019 09:45:38 +0100 Subject: [PATCH 7/9] Update map-filter.js --- ghufran/map-filter.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ghufran/map-filter.js b/ghufran/map-filter.js index 084bc0f98..f587a1fdf 100644 --- a/ghufran/map-filter.js +++ b/ghufran/map-filter.js @@ -2,10 +2,14 @@ function doubleOddNumbers(numbers) { const newNumbers = numbers.filter(number => number % 2 !== 0); // filter the odd nrs - const doubleNumbers = newNumbers.map(number => number * 2); // double the odd nrs - return doubleNumbers; + numbers = newNumbers.map(number => number * 2); // double the odd nrs + console.log(numbers); } const myNumbers = [1, 2, 3, 4]; console.log(doubleOddNumbers(myNumbers)); // [2, 6] the double of [1,3] +module.exports = { + myNumbers, + doubleOddNumbers, +}; From 7d15291c7e929c06f729409a40e30cfd0deead43 Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Thu, 9 May 2019 23:12:20 +0200 Subject: [PATCH 8/9] Update maartjes-work.js --- ghufran/maartjes-work.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ghufran/maartjes-work.js b/ghufran/maartjes-work.js index e420a496b..8a9593f4e 100644 --- a/ghufran/maartjes-work.js +++ b/ghufran/maartjes-work.js @@ -46,11 +46,10 @@ const maartjesTasks = monday.concat(tuesday); const maartjesHourlyRate = 20; function computeEarnings(tasks, hourlyRate) { - const duration = tasks.map(task => { // turn mins to hours - const hours = task.duration / 60; + const duration = tasks.map(task => task.duration / 60); return { ...task, duration: hours }; - }); + }; // remove less than 2 hours const filteredHour = duration.filter(elem => elem.duration >= 2); From 6e722a5a1ced7e2fd91e7aa3811db0083fc73b6e Mon Sep 17 00:00:00 2001 From: ghifo <41265609+ghifo@users.noreply.github.com> Date: Sat, 11 May 2019 22:42:54 +0200 Subject: [PATCH 9/9] Update map-filter.js --- ghufran/map-filter.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ghufran/map-filter.js b/ghufran/map-filter.js index f587a1fdf..651fb6ae7 100644 --- a/ghufran/map-filter.js +++ b/ghufran/map-filter.js @@ -1,9 +1,8 @@ 'use strict'; -function doubleOddNumbers(numbers) { - const newNumbers = numbers.filter(number => number % 2 !== 0); // filter the odd nrs - numbers = newNumbers.map(number => number * 2); // double the odd nrs - console.log(numbers); +function doubleOddNumbers(num) { + num = num.filter(n => n % 2 !== 0).map(number => number * 2); + return num; } const myNumbers = [1, 2, 3, 4];