From 310909d19b1b4fb1244a0addd0577d42e482d5ad Mon Sep 17 00:00:00 2001 From: reiyon Date: Wed, 3 Mar 2021 10:35:59 +0100 Subject: [PATCH 1/3] Finish map-filter.js and maartijes-work.js tasks --- Week2/homework/maartjes-work.js | 13 ++++++++----- Week2/homework/map-filter.js | 8 ++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Week2/homework/maartjes-work.js b/Week2/homework/maartjes-work.js index 49772eb44..fc81fe3c7 100644 --- a/Week2/homework/maartjes-work.js +++ b/Week2/homework/maartjes-work.js @@ -46,16 +46,19 @@ const maartjesTasks = monday.concat(tuesday); const maartjesHourlyRate = 20; function computeEarnings(tasks, hourlyRate) { - // Replace this comment and the next line with your code - console.log(tasks, hourlyRate); + const durationsInHours = tasks.map(task => task.duration / 60); + const durationsForCalc = durationsInHours.filter(duration => duration >= 2); + const earnings = durationsForCalc.map(duration => duration * hourlyRate); + const totalEarnings = earnings.reduce((total, earning) => total + earning, 0); + return totalEarnings; } +console.log(computeEarnings(maartjesTasks, maartjesHourlyRate)); // eslint-disable-next-line no-unused-vars const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); - +const rounded = earnings.toFixed(2); // add code to convert `earnings` to a string rounded to two decimals (euro cents) - -console.log(`Maartje has earned €${'replace this string with the earnings rounded to euro cents'}`); +console.log(`Maartje has earned €${rounded}`); // Do not change or remove anything below this line module.exports = { diff --git a/Week2/homework/map-filter.js b/Week2/homework/map-filter.js index c8e8a88c1..55f91855d 100644 --- a/Week2/homework/map-filter.js +++ b/Week2/homework/map-filter.js @@ -1,9 +1,9 @@ 'use strict'; -function doubleOddNumbers(numbers) { - // Replace this comment and the next line with your code - console.log(numbers); -} +const doubleOddNumbers = numbers => { + const oddNumbers = numbers.filter(number => number % 2 !== 0); + return oddNumbers.map(oddNumber => oddNumber * 2); +}; const myNumbers = [1, 2, 3, 4]; console.log(doubleOddNumbers(myNumbers)); From 41d11f88f0694fc9469169790d038616e43d3765 Mon Sep 17 00:00:00 2001 From: reiyon Date: Wed, 3 Mar 2021 12:22:25 +0100 Subject: [PATCH 2/3] Finish JSON parsing task --- Week2/homework/index.html | 12 ++++++++++++ Week2/homework/squirtle-sprites.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 Week2/homework/index.html diff --git a/Week2/homework/index.html b/Week2/homework/index.html new file mode 100644 index 000000000..71a8e2aa5 --- /dev/null +++ b/Week2/homework/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/Week2/homework/squirtle-sprites.js b/Week2/homework/squirtle-sprites.js index b6b6e2920..016cf7c19 100644 --- a/Week2/homework/squirtle-sprites.js +++ b/Week2/homework/squirtle-sprites.js @@ -9,3 +9,21 @@ function fetchPokemonData() { } /* Code goes below */ +const data = JSON.parse(fetchPokemonData()); + +const displaySprites = () => { + const section = document.createElement('section'); + + const spritesKeys = Object.keys(data.sprites); + spritesKeys.forEach(key => { + const img = document.createElement('img'); + if (data.sprites[key]) { + img.src = data.sprites[key]; + section.appendChild(img); + } + }); + + document.body.appendChild(section); +}; + +displaySprites(); From 2c5bbb0d99a9af4cea2828c349441c37d1501cfa Mon Sep 17 00:00:00 2001 From: reiyon Date: Tue, 9 Mar 2021 08:46:01 +0100 Subject: [PATCH 3/3] Refactor --- Week2/homework/maartjes-work.js | 17 ++++++++--------- Week2/homework/map-filter.js | 6 ++---- Week2/homework/squirtle-sprites.js | 27 ++++++++++++++++++++------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Week2/homework/maartjes-work.js b/Week2/homework/maartjes-work.js index fc81fe3c7..98563f5d5 100644 --- a/Week2/homework/maartjes-work.js +++ b/Week2/homework/maartjes-work.js @@ -42,23 +42,22 @@ const tuesday = [ }, ]; -const maartjesTasks = monday.concat(tuesday); +const maartjesTasks = [...monday, ...tuesday]; const maartjesHourlyRate = 20; function computeEarnings(tasks, hourlyRate) { - const durationsInHours = tasks.map(task => task.duration / 60); - const durationsForCalc = durationsInHours.filter(duration => duration >= 2); - const earnings = durationsForCalc.map(duration => duration * hourlyRate); - const totalEarnings = earnings.reduce((total, earning) => total + earning, 0); - return totalEarnings; + return tasks + .map(task => task.duration / 60) + .filter(duration => duration >= 2) + .map(duration => duration * hourlyRate) + .reduce((total, earning) => total + earning, 0); } console.log(computeEarnings(maartjesTasks, maartjesHourlyRate)); // eslint-disable-next-line no-unused-vars -const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); -const rounded = earnings.toFixed(2); +const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate).toFixed(2); // add code to convert `earnings` to a string rounded to two decimals (euro cents) -console.log(`Maartje has earned €${rounded}`); +console.log(`Maartje has earned €${earnings}`); // Do not change or remove anything below this line module.exports = { diff --git a/Week2/homework/map-filter.js b/Week2/homework/map-filter.js index 55f91855d..ef5755c0f 100644 --- a/Week2/homework/map-filter.js +++ b/Week2/homework/map-filter.js @@ -1,9 +1,7 @@ 'use strict'; -const doubleOddNumbers = numbers => { - const oddNumbers = numbers.filter(number => number % 2 !== 0); - return oddNumbers.map(oddNumber => oddNumber * 2); -}; +const doubleOddNumbers = numbers => + numbers.filter(number => number % 2 !== 0).map(oddNumber => oddNumber * 2); const myNumbers = [1, 2, 3, 4]; console.log(doubleOddNumbers(myNumbers)); diff --git a/Week2/homework/squirtle-sprites.js b/Week2/homework/squirtle-sprites.js index 016cf7c19..e63c34fc7 100644 --- a/Week2/homework/squirtle-sprites.js +++ b/Week2/homework/squirtle-sprites.js @@ -9,19 +9,32 @@ function fetchPokemonData() { } /* Code goes below */ -const data = JSON.parse(fetchPokemonData()); const displaySprites = () => { + const data = JSON.parse(fetchPokemonData()); + const { sprites } = data; const section = document.createElement('section'); - const spritesKeys = Object.keys(data.sprites); - spritesKeys.forEach(key => { + for (const key in sprites) { + const div = document.createElement('div'); const img = document.createElement('img'); - if (data.sprites[key]) { - img.src = data.sprites[key]; - section.appendChild(img); + if (sprites[key]) { + img.src = sprites[key]; + section.appendChild(div); + div.appendChild(img); } - }); + } + // const spritesKeys = Object.keys(sprites); + // console.log(spritesKeys); + // spritesKeys.forEach(key => { + // const div = document.createElement('div'); + // const img = document.createElement('img'); + // if (sprites[key]) { + // img.src = sprites[key]; + // section.appendChild(div); + // div.appendChild(img); + // } + // }); document.body.appendChild(section); };