diff --git a/Week2/homework/maartjes-work.js b/Week2/homework/maartjes-work.js index 49772eb44..ce92a5d72 100644 --- a/Week2/homework/maartjes-work.js +++ b/Week2/homework/maartjes-work.js @@ -47,15 +47,21 @@ const maartjesHourlyRate = 20; function computeEarnings(tasks, hourlyRate) { // Replace this comment and the next line with your code - console.log(tasks, hourlyRate); + const tasksRate = tasks + .map(time => time.duration / 60) // map durations in hours + .filter(time => time >= 2) // remove the duration, that is < 2 hours + .map(time => time * hourlyRate) // multiply each duration per hour (rate =20) + .reduce((total, value) => total + value); // Total sum all + return tasksRate; } // eslint-disable-next-line no-unused-vars const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate); // add code to convert `earnings` to a string rounded to two decimals (euro cents) +const result = earnings.toFixed(2); -console.log(`Maartje has earned €${'replace this string with the earnings rounded to euro cents'}`); +console.log(`Maartje has earned €${result}`); // 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..f98cbb888 100644 --- a/Week2/homework/map-filter.js +++ b/Week2/homework/map-filter.js @@ -1,8 +1,12 @@ 'use strict'; function doubleOddNumbers(numbers) { - // Replace this comment and the next line with your code - console.log(numbers); + // Replace this comment and the next line with your code + //filtered even numbers + const oddNumber = numbers.filter((odd) => odd % 2 !== 0); + //multiply the value to 2 using map + numbers = oddNumber.map((double) => double * 2); + return numbers; } const myNumbers = [1, 2, 3, 4]; @@ -10,6 +14,6 @@ console.log(doubleOddNumbers(myNumbers)); // Do not change or remove anything below this line module.exports = { - myNumbers, - doubleOddNumbers, -}; + myNumbers, + doubleOddNumbers, +}; \ No newline at end of file diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..6feb15327 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -1,16 +1,18 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; function foo(func) { - // What to do here? - // Replace this comment and the next line with your code - console.log(func); + // What to do here? + // Replace this comment and the next line with your code + func(); } function bar() { - console.log('Hello, I am bar!'); + console.log('Hello, I am bar!'); } foo(bar); // Do not change or remove anything below this line -module.exports = foo; +module.exports = foo; \ No newline at end of file diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..8e89e00ba 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -1,23 +1,37 @@ +/* eslint-disable eqeqeq */ +/* eslint-disable no-use-before-define */ +/* eslint-disable prettier/prettier */ + 'use strict'; function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { - const numbers = []; + const numbers = []; + + for (let i = startIndex; i <= stopIndex; i++) { numbers.push(i); } + + numbers.forEach(number => { + if (number % 3 === 0 && number % 5 === 0) { + threeCallback(number); + fiveCallback(number); + } else if (number % 3 === 0) { + threeCallback(number); + } else if (number % 5 === 0) { + fiveCallback(number); + } + }); - // Replace this comment and the next line with your code - console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers); } function sayThree(number) { - // Replace this comment and the next line with your code - console.log(number); + // eslint-disable-next-line eqeqeq + console.log(number + ' is multiplication of three.'); } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(number + ' is multiplication of five.'); } threeFive(10, 15, sayThree, sayFive); // Do not change or remove anything below this line -module.exports = threeFive; +module.exports = threeFive; \ No newline at end of file diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index 00845c5eb..27e153dee 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -1,47 +1,54 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; // Use a 'for' loop 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); + // eslint-disable-next-line prefer-const + let result = ''; - return result; + for (let i = 0; i < num; i++) { + result += `${str}`; + } + return result; } console.log('for', repeatStringNumTimesWithFor('abc', 3)); // Use a 'while' loop 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); - - return result; + // eslint-disable-next-line prefer-const + let result = ''; + let i = 0; + + while (i < num) { + result += `${str}`; + i++; + } + return result; } console.log('while', repeatStringNumTimesWithWhile('abc', 3)); // Use a 'do...while' loop 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); - - return result; + let result = ''; + let i = 0; + if (i < num) { + do { + i++; + result += `${str}`; + } while (i < num); + } + + return result; } console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3)); // Do not change or remove anything below this line module.exports = { - repeatStringNumTimesWithFor, - repeatStringNumTimesWithWhile, - repeatStringNumTimesWithDoWhile, -}; + repeatStringNumTimesWithFor, + repeatStringNumTimesWithWhile, + repeatStringNumTimesWithDoWhile, +} \ No newline at end of file diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..1cad85c40 100644 --- a/Week3/homework/step2-4.js +++ b/Week3/homework/step2-4.js @@ -1,10 +1,16 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; function Dog() { - // add your code here + // add your code here + this.name = 'Pluto'; + this.color = 'brown'; + this.numLegs = 4; } const hound = new Dog(); +console.log(hound); // Do not change or remove anything below this line -module.exports = hound; +module.exports = hound; \ No newline at end of file diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..88bc7e09d 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -1,17 +1,26 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; function multiplyAll(arr) { - // eslint-disable-next-line - let product = 1; + // 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 = product * arr[i][j]; + } + } - return product; + return product; } -const result = multiplyAll([[1, 2], [3, 4], [5, 6]]); +const result = multiplyAll([ + [1, 2], + [3, 4], + [5, 6] +]); console.log(result); // 720 // Do not change or remove anything below this line -module.exports = multiplyAll; +module.exports = multiplyAll; \ No newline at end of file diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index ffe95b9f7..2bc64c764 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -1,16 +1,40 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; -const arr2d = [[1, 2], [3, 4], [5, 6]]; -const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; +const arr2d = [ + [1, 2], + [3, 4], + [5, 6] +]; +const arr3d = [ + [ + [1, 2], + [3, 4] + ], + [ + [5, 6], + [7, 8] + ] +]; function flattenArray2d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + // return arr.reduce((acc, val) => acc.concat(val), []); + const newArr = []; + for (let i = 0; i < arr.length; i++) { + newArr.push(...arr[i]); + } + return newArr; } function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + // arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenArray3d(val)) : acc.concat(val), []); + const newArr = []; + + for (let i = 0; i < arr.length; i++) { + newArr.push(...arr[i]); + } + return flattenArray2d(newArr); } console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6] @@ -18,6 +42,6 @@ console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8] // Do not change or remove anything below this line module.exports = { - flattenArray2d, - flattenArray3d, -}; + flattenArray2d, + flattenArray3d, +}; \ No newline at end of file diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..d1cd94b6a 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -1,9 +1,12 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; const x = 9; + function f1(val) { - val = val + 1; - return val; + val = val + 1; + return val; } f1(x); @@ -11,9 +14,10 @@ f1(x); console.log(x); const y = { x: 9 }; + function f2(val) { - val.x = val.x + 1; - return val; + val.x = val.x + 1; + return val; } f2(y); @@ -21,3 +25,6 @@ f2(y); console.log(y); // Add your explanation as a comment here +// f1 passed argument from x to val and print x out. But x type is const , that's why the result is 9. +// f2 passed argument from const y by using constructor val.x +// We can't change const value, but by using constructor can change the value of x and print out x:10 \ No newline at end of file diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..092ae1089 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -1,14 +1,16 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; 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 filterArray = () => [...new Set(arr)] + return filterArray(); } const uniqueValues = makeUnique(values); console.log(uniqueValues); // Do not change or remove anything below this line -module.exports = makeUnique; +module.exports = makeUnique; \ No newline at end of file diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..8c0a7a56d 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/step3.js @@ -1,8 +1,11 @@ +/* eslint-disable prettier/prettier */ + 'use strict'; function createBase(base) { - // Replace this comment and the next line with your code - console.log(base); + return function(addition) { + return base + addition + } } const addSix = createBase(6); @@ -11,4 +14,4 @@ console.log(addSix(10)); // returns 16 console.log(addSix(21)); // returns 27 // Do not change or remove anything below this line -module.exports = createBase; +module.exports = createBase; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index b3043a79d..2c86e3861 100644 --- a/package-lock.json +++ b/package-lock.json @@ -598,7 +598,7 @@ }, "array-equal": { "version": "1.0.0", - "resolved": "http://registry.npmjs.org/array-equal/-/array-equal-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/array-equal/-/array-equal-1.0.0.tgz", "integrity": "sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM=", "dev": true }, @@ -5112,7 +5112,7 @@ }, "safe-regex": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", "integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", "dev": true, "requires": { @@ -5547,7 +5547,7 @@ }, "strip-eof": { "version": "1.0.0", - "resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", "dev": true },