From f32d9e91a55e6173a53631b7f7143ce62a8dac1f Mon Sep 17 00:00:00 2001 From: Kurnia Date: Fri, 12 Mar 2021 01:15:41 +0100 Subject: [PATCH 1/6] test successfully --- Week2/homework/maartjes-work.js | 94 +++++++++++++++++---------------- Week2/homework/map-filter.js | 14 +++-- package-lock.json | 6 +-- 3 files changed, 61 insertions(+), 53 deletions(-) diff --git a/Week2/homework/maartjes-work.js b/Week2/homework/maartjes-work.js index 49772eb44..ad0fa1b6b 100644 --- a/Week2/homework/maartjes-work.js +++ b/Week2/homework/maartjes-work.js @@ -1,65 +1,69 @@ '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 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, - }, +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, + }, ]; 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); + // Replace this comment and the next line with your code + 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 = { - maartjesTasks, - maartjesHourlyRate, - computeEarnings, -}; + maartjesTasks, + maartjesHourlyRate, + computeEarnings, +}; \ No newline at end of file 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/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 }, From 22fe9312ab637ba568ffa270f661b307a101772b Mon Sep 17 00:00:00 2001 From: Kurnia Date: Sat, 20 Mar 2021 00:48:06 +0100 Subject: [PATCH 2/6] hw week 3 or 4 --- Week2/homework/maartjes-work.js | 96 +++++++++++++++++---------------- Week3/homework/step2-1.js | 12 ++--- Week3/homework/step2-2.js | 31 ++++++++--- Week3/homework/step2-3.js | 45 +++++++++------- Week3/homework/step2-4.js | 10 +++- Week3/homework/step2-5.js | 23 +++++--- Week3/homework/step2-6.js | 31 +++++++---- Week3/homework/step2-7.js | 15 ++++-- Week3/homework/step3-bonus.js | 8 +-- Week3/homework/step3.js | 9 ++-- 10 files changed, 172 insertions(+), 108 deletions(-) diff --git a/Week2/homework/maartjes-work.js b/Week2/homework/maartjes-work.js index ad0fa1b6b..ce92a5d72 100644 --- a/Week2/homework/maartjes-work.js +++ b/Week2/homework/maartjes-work.js @@ -1,56 +1,58 @@ '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 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, - }, +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, + }, ]; const maartjesTasks = monday.concat(tuesday); const maartjesHourlyRate = 20; function computeEarnings(tasks, hourlyRate) { - // Replace this comment and the next line with your code - 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; + // Replace this comment and the next line with your code + 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 @@ -63,7 +65,7 @@ console.log(`Maartje has earned €${result}`); // Do not change or remove anything below this line module.exports = { - maartjesTasks, - maartjesHourlyRate, - computeEarnings, -}; \ No newline at end of file + maartjesTasks, + maartjesHourlyRate, + computeEarnings, +}; diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..2955cac5e 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -1,16 +1,16 @@ '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 + return func; } function bar() { - console.log('Hello, I am bar!'); + console.log('Hello, I am bar!'); } -foo(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..a594b4025 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -1,23 +1,38 @@ +/* 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 => { + // eslint-disable-next-line no-func-assign + sayThree = threeCallback(number); + // eslint-disable-next-line no-func-assign + sayFive = 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 + if (number % 3 == 0) { + console.log('Say three!'); + } } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + if (number % 5 == 0) { + console.log('Say 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..de976a6df 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 = ''; + // 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 = str; num > 0; num--) { + result += i; + } - return result; + return result; } console.log('for', repeatStringNumTimesWithFor('abc', 3)); // Use a 'while' loop function repeatStringNumTimesWithWhile(str, num) { - // eslint-disable-next-line prefer-const - let result = ''; + // 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; + 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 = ''; + // 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; + 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..4d1810b43 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -1,16 +1,29 @@ +/* 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), []); } 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), []); } console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6] @@ -18,6 +31,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 From fa3a2261574a948f551a6d1f0622d500709e449f Mon Sep 17 00:00:00 2001 From: Kurnia Date: Sun, 21 Mar 2021 00:13:26 +0100 Subject: [PATCH 3/6] fixing 2-1 --- Week3/homework/step2-1.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index 2955cac5e..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 - return func; + func(); } function bar() { console.log('Hello, I am bar!'); } -foo(bar()); +foo(bar); // Do not change or remove anything below this line module.exports = foo; \ No newline at end of file From 0b22f076f664e67ffa69a94b52a2b06073f27494 Mon Sep 17 00:00:00 2001 From: Kurnia Date: Sun, 21 Mar 2021 00:13:37 +0100 Subject: [PATCH 4/6] fixing 2-2 --- Week3/homework/step2-2.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index a594b4025..8e89e00ba 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -10,26 +10,25 @@ function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { for (let i = startIndex; i <= stopIndex; i++) { numbers.push(i); } numbers.forEach(number => { - // eslint-disable-next-line no-func-assign - sayThree = threeCallback(number); - // eslint-disable-next-line no-func-assign - sayFive = fiveCallback(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); + } }); } function sayThree(number) { // eslint-disable-next-line eqeqeq - if (number % 3 == 0) { - console.log('Say three!'); - } + console.log(number + ' is multiplication of three.'); } function sayFive(number) { - if (number % 5 == 0) { - console.log('Say five!'); - } + console.log(number + ' is multiplication of five.'); } threeFive(10, 15, sayThree, sayFive); From 228e6e49ba6d751d11f300431135f7d712b69087 Mon Sep 17 00:00:00 2001 From: Kurnia Date: Sun, 21 Mar 2021 00:13:44 +0100 Subject: [PATCH 5/6] fixing 2-3 --- Week3/homework/step2-3.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index de976a6df..27e153dee 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -7,10 +7,9 @@ function repeatStringNumTimesWithFor(str, num) { // eslint-disable-next-line prefer-const let result = ''; - for (let i = str; num > 0; num--) { - result += i; + for (let i = 0; i < num; i++) { + result += `${str}`; } - return result; } @@ -20,12 +19,12 @@ console.log('for', repeatStringNumTimesWithFor('abc', 3)); function repeatStringNumTimesWithWhile(str, num) { // eslint-disable-next-line prefer-const let result = ''; + let i = 0; - while (num > 0) { - result += str; - num--; + while (i < num) { + result += `${str}`; + i++; } - return result; } @@ -33,13 +32,14 @@ console.log('while', repeatStringNumTimesWithWhile('abc', 3)); // Use a 'do...while' loop function repeatStringNumTimesWithDoWhile(str, num) { - // eslint-disable-next-line prefer-const let result = ''; - - do { - result += str; - num--; - } while (num > 0); + let i = 0; + if (i < num) { + do { + i++; + result += `${str}`; + } while (i < num); + } return result; } @@ -51,4 +51,4 @@ module.exports = { repeatStringNumTimesWithFor, repeatStringNumTimesWithWhile, repeatStringNumTimesWithDoWhile, -}; \ No newline at end of file +} \ No newline at end of file From 6adce5d2f436681469948632d6f1df2984f384b4 Mon Sep 17 00:00:00 2001 From: Kurnia Date: Sun, 21 Mar 2021 00:13:51 +0100 Subject: [PATCH 6/6] fixing 2-6 --- Week3/homework/step2-6.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index 4d1810b43..2bc64c764 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -19,11 +19,22 @@ const arr3d = [ ]; function flattenArray2d(arr) { - return arr.reduce((acc, val) => acc.concat(val), []); + // 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) { - arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenArray3d(val)) : acc.concat(val), []); + // 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]