diff --git a/Week2/homework/maartjes-work.js b/Week2/homework/maartjes-work.js index 49772eb44..f85a399d4 100644 --- a/Week2/homework/maartjes-work.js +++ b/Week2/homework/maartjes-work.js @@ -45,17 +45,50 @@ const tuesday = [ 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); -} +-------------------------------------------First Solustion------------------------------------------------------------------ +//Map the tasks to durations in hours. +let tasksInHour = maartjesTasks.map(tasks => tasks.duration / 60); + +//Filter out everything that took less than two hours +let longTasks = tasksInHour.filter(task => { + return task >= 2; +}); + +//Multiply the each duration by a per-hour rate for billing (use €20/hour) and sum it all up +let profits = longTasks.map(task => { + return task * 20; +}); + +let totalProfit = 0; +profits.forEach(function(profit) { + totalProfit += profit; +}); -// eslint-disable-next-line no-unused-vars -const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); +//Output a formatted Euro amount, rounded to Euro cents, e.g: €11.34 +let roundedTotalProfit = totalProfit.toFixed(2); // add code to convert `earnings` to a string rounded to two decimals (euro cents) +console.log(`Maartje has earned €${roundedTotalProfit}`); + + +------------------------------------------Second Solution------------------------------------------------------------------- +function computeEarnings(tasks, hourlyRate) { + let profits = []; + tasks.forEach(function(task) { + let duration = task.duration / 60; + if (duration >= 2) { + profits.push(duration * hourlyRate); + } + }); + let totalProfits = profits.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + return totalProfits; + } + + // eslint-disable-next-line no-unused-vars + const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate).toFixed(2); -console.log(`Maartje has earned €${'replace this string with the earnings rounded to euro cents'}`); + // add code to convert `earnings` to a string rounded to two decimals (euro cents) + 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 c8e8a88c1..861f10447 100644 --- a/Week2/homework/map-filter.js +++ b/Week2/homework/map-filter.js @@ -1,13 +1,20 @@ 'use strict'; - -function doubleOddNumbers(numbers) { - // Replace this comment and the next line with your code - console.log(numbers); +//1.1 Rewrite the above doubleOddNumbers function using map and filter; don't forget to use =>. +function doubleOddNumbers(myNumbers) { + let oddNumbers = []; + for (let i = 0; i < myNumbers.length; i++) { + if (myNumbers[i] % 2 !== 0) { + oddNumbers.push(myNumbers[i] * 2); + } + } + return oddNumbers; } - const myNumbers = [1, 2, 3, 4]; console.log(doubleOddNumbers(myNumbers)); +//Rewrite the above doubleOddNumbers function using map and filter; don't forget to use =>. +const myOddNumbers = myNumbers.filter(myNumber => myNumber % 2 !== 0).map(myNumber => myNumber * 2); +console.log(myOddNumbers); // Do not change or remove anything below this line module.exports = { myNumbers, diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..28fa47a4b 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -1,9 +1,7 @@ 'use strict'; function foo(func) { - // What to do here? - // Replace this comment and the next line with your code - console.log(func); + func(); } function bar() { diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..f9c57fc8b 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -2,19 +2,25 @@ function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { const numbers = []; + for (let number = startIndex; number <= stopIndex; number++) { + numbers.push(number); + if (number % 3 === 0) { + sayThree(number); + } + if (number % 5 === 0) { + sayFive(number); + } + } - // Replace this comment and the next line with your code - console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers); + console.log(numbers); } function sayThree(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(sayThree); } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(sayFive); } threeFive(10, 15, sayThree, sayFive); diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index 00845c5eb..0b5b39b6c 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -4,13 +4,14 @@ function repeatStringNumTimesWithFor(str, num) { // eslint-disable-next-line prefer-const let result = ''; - - // Replace this comment and the next line with your code - console.log(str, num, result); - + for (let i = 0; i < num; i++) { + result += str; + if (i === num) { + return result; + } + } return result; } - console.log('for', repeatStringNumTimesWithFor('abc', 3)); // Use a 'while' loop @@ -18,8 +19,10 @@ function repeatStringNumTimesWithWhile(str, num) { // eslint-disable-next-line prefer-const let result = ''; - // Replace this comment and the next line with your code - console.log(str, num, result); + while (num > 0) { + result += str; + num--; + } return result; } @@ -31,8 +34,10 @@ function repeatStringNumTimesWithDoWhile(str, num) { // eslint-disable-next-line prefer-const let result = ''; - // Replace this comment and the next line with your code - console.log(str, num, result); + do { + result += str; + num--; + } while (num > 0); return result; } diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..2dab07e87 100644 --- a/Week3/homework/step2-4.js +++ b/Week3/homework/step2-4.js @@ -1,10 +1,15 @@ 'use strict'; function Dog() { - // add your code here + this.name = 'Carry'; + this.color = 'black'; + this.numLegs = 4; } const hound = new Dog(); +hound.name = 'Isabella'; +hound.color = 'Gray'; +hound.numLegs = 4; // Do not change or remove anything below this line module.exports = hound; diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..ff7d18590 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -3,9 +3,11 @@ function multiplyAll(arr) { // eslint-disable-next-line let product = 1; - - // Replace this comment and the next line with your code - console.log(arr, product); + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + product *= arr[i][j]; + } + } return product; } diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index ffe95b9f7..dd7e501dd 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -3,14 +3,27 @@ const arr2d = [[1, 2], [3, 4], [5, 6]]; const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; +let my2dArr = []; + function flattenArray2d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + for (let i = 0; i < arr2d.length; i++) { + for (let j = 0; j < arr2d[i].length; j++) { + my2dArr.push(arr2d[i][j]); + } + } + return my2dArr; } +let my3dArr = []; function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + for (let i = 0; i < arr3d.length; i++) { + for (let j = 0; j < arr3d[i].length; j++) { + for (let k = 0; k < arr3d[j].length; k++) { + my3dArr.push(arr3d[i][j][k]); + } + } + } + return my3dArr; } console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6] diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..357e6cc64 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -21,3 +21,9 @@ f2(y); console.log(y); // Add your explanation as a comment here + +/* In the first function: +- The variable x is assign to 9, the function increment 9 to 10. But since x is a const variable when console.log calls for x, the function prints out the const variable value which is 9. + In the second function: +- variable y is an object, the function use the key x to change the value of 9. It increment it to 10 and assign it to x. when the console.log calls for y, variable y finds the x and the value 10 which is assigned to it. */ + diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..1ebeaddb7 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -3,10 +3,16 @@ const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c']; function makeUnique(arr) { - // Replace this comment and the next line with your code - console.log(arr); +const myUniqueArray = []; + arr.forEach(function(element) { + if (myUniqueArray.indexOf(element) === -1) { + myUniqueArray.push(element); + } + }); + return myUniqueArray; } + const uniqueValues = makeUnique(values); console.log(uniqueValues); diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..29c1c2c2a 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/step3.js @@ -1,8 +1,7 @@ 'use strict'; function createBase(base) { - // Replace this comment and the next line with your code - console.log(base); + return base + 6; } const addSix = createBase(6);