From d2d2354a33ad58d883e983381093e567c2a2b8c2 Mon Sep 17 00:00:00 2001 From: A T Date: Fri, 26 Jul 2019 13:05:22 +0200 Subject: [PATCH 1/7] 2-1 for week3 --- Week3/homework/MY-index.html | 12 ++++++++++++ Week3/homework/{step2-1.js => MY-step2-1.js} | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Week3/homework/MY-index.html rename Week3/homework/{step2-1.js => MY-step2-1.js} (55%) diff --git a/Week3/homework/MY-index.html b/Week3/homework/MY-index.html new file mode 100644 index 000000000..f18eac594 --- /dev/null +++ b/Week3/homework/MY-index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/Week3/homework/step2-1.js b/Week3/homework/MY-step2-1.js similarity index 55% rename from Week3/homework/step2-1.js rename to Week3/homework/MY-step2-1.js index d5699882c..326f2a037 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/MY-step2-1.js @@ -1,15 +1,22 @@ 'use strict'; +/*sample*/ +// function doIt() { +// console.log('I am done'); +// } +// setTimeout(doIt, 5000); +//Test by copy-pasting this function to the browser console function foo(func) { // What to do here? // Replace this comment and the next line with your code + return ('Hi, I am AaaaT, who are you?'); console.log(func); } function bar() { console.log('Hello, I am bar!'); } - +setTimeout(bar, 3000); foo(bar); // Do not change or remove anything below this line From c74a0253ea5d2ae4c1cc5f4bfefa14ccb86316d3 Mon Sep 17 00:00:00 2001 From: A T Date: Fri, 26 Jul 2019 13:07:49 +0200 Subject: [PATCH 2/7] comment ex2-1 --- Week3/homework/MY-step2-1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week3/homework/MY-step2-1.js b/Week3/homework/MY-step2-1.js index 326f2a037..9c8294c9e 100644 --- a/Week3/homework/MY-step2-1.js +++ b/Week3/homework/MY-step2-1.js @@ -5,7 +5,7 @@ // } // setTimeout(doIt, 5000); -//Test by copy-pasting this function to the browser console +//Test this code by copy-pasting the whole foo and bar functions to the browser console function foo(func) { // What to do here? // Replace this comment and the next line with your code From c0d39baa0f4c83c46b8a68a14fba4d8fe1983a4c Mon Sep 17 00:00:00 2001 From: AaaaT Date: Fri, 26 Jul 2019 13:44:31 +0200 Subject: [PATCH 3/7] Rename MY-step2-1.js to MY-step2-1-Hello_I_am_bar.js --- Week3/homework/{MY-step2-1.js => MY-step2-1-Hello_I_am_bar.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week3/homework/{MY-step2-1.js => MY-step2-1-Hello_I_am_bar.js} (100%) diff --git a/Week3/homework/MY-step2-1.js b/Week3/homework/MY-step2-1-Hello_I_am_bar.js similarity index 100% rename from Week3/homework/MY-step2-1.js rename to Week3/homework/MY-step2-1-Hello_I_am_bar.js From bad3e60c312b69ec85cf7f8acde3eaafb3c37c5d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 12 Aug 2019 13:05:21 +0200 Subject: [PATCH 4/7] ex 2-2, 2-3, 2-4, 2-5 --- Week3/homework/MY-index.html | 5 ++ Week3/homework/MY-step2-1-Hello_I_am_bar.js | 2 +- Week3/homework/MY-step2-2-Func_with_4_Arg.js | 49 +++++++++++++++++++ ... MY-step2-4-Some-practice-with-objects.js} | 4 ++ ...tep2-5.js => MY-step2-5-Nest-for-loops.js} | 13 +++-- Week3/homework/step2-2.js | 23 --------- 6 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 Week3/homework/MY-step2-2-Func_with_4_Arg.js rename Week3/homework/{step2-4.js => MY-step2-4-Some-practice-with-objects.js} (69%) rename Week3/homework/{step2-5.js => MY-step2-5-Nest-for-loops.js} (54%) delete mode 100644 Week3/homework/step2-2.js diff --git a/Week3/homework/MY-index.html b/Week3/homework/MY-index.html index f18eac594..1d441d38e 100644 --- a/Week3/homework/MY-index.html +++ b/Week3/homework/MY-index.html @@ -8,5 +8,10 @@ + + + + + \ No newline at end of file diff --git a/Week3/homework/MY-step2-1-Hello_I_am_bar.js b/Week3/homework/MY-step2-1-Hello_I_am_bar.js index 9c8294c9e..a6e0a422e 100644 --- a/Week3/homework/MY-step2-1-Hello_I_am_bar.js +++ b/Week3/homework/MY-step2-1-Hello_I_am_bar.js @@ -10,7 +10,7 @@ function foo(func) { // What to do here? // Replace this comment and the next line with your code return ('Hi, I am AaaaT, who are you?'); - console.log(func); + //console.log(func); } function bar() { diff --git a/Week3/homework/MY-step2-2-Func_with_4_Arg.js b/Week3/homework/MY-step2-2-Func_with_4_Arg.js new file mode 100644 index 000000000..a365de167 --- /dev/null +++ b/Week3/homework/MY-step2-2-Func_with_4_Arg.js @@ -0,0 +1,49 @@ +'use strict'; + +/*2.2 You must write a function that takes 4 arguments. + +A start value +An end value +A callback to call if the number is divisible by 3 +A callback to use if the number is divisible by 5 +The function should first generate an array containing values from start value to end value (inclusive). + +Then the function should take the newly created array and iterate over it, and calling the first callback if the array value is divisible by 3. + +The function should call the second callback if the array value is divisible by 5. + +Both functions should be called if the array value is divisible by both 3 and 5. */ + +function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { + const numbers = []; + // Replace this comment and the next line with your code + for (let i = startIndex; i <= stopIndex; i++) { + numbers.push(i); + //A callback to call if the number is divisible by 3 + if (i % 3 === 0) { + threeCallback(i); + } + //A callback to use if the number is divisible by 5 + if (i % 5 === 0) { + fiveCallback(i); + } + } + } + +threeFive() + + +function sayThree(number) { + // Replace this comment and the next line with your code + console.log(number, 'value is divisible by 3'); +} + +function sayFive(number) { + // Replace this comment and the next line with your code + console.log(number, 'value is divisible by 5'); +} + +threeFive(10, 15, sayThree, sayFive); + +// Do not change or remove anything below this line +module.exports = threeFive; diff --git a/Week3/homework/step2-4.js b/Week3/homework/MY-step2-4-Some-practice-with-objects.js similarity index 69% rename from Week3/homework/step2-4.js rename to Week3/homework/MY-step2-4-Some-practice-with-objects.js index b11b1dcb6..165c36f57 100644 --- a/Week3/homework/step2-4.js +++ b/Week3/homework/MY-step2-4-Some-practice-with-objects.js @@ -2,6 +2,10 @@ function Dog() { // add your code here + this.name = "Woofy Boy", + this.color = "Brown", + this.numLegs = 4; + } const hound = new Dog(); diff --git a/Week3/homework/step2-5.js b/Week3/homework/MY-step2-5-Nest-for-loops.js similarity index 54% rename from Week3/homework/step2-5.js rename to Week3/homework/MY-step2-5-Nest-for-loops.js index cbb54fa1d..95e1f4456 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/MY-step2-5-Nest-for-loops.js @@ -1,17 +1,22 @@ -'use strict'; +//'use strict'; function multiplyAll(arr) { // eslint-disable-next-line let product = 1; - // Replace this comment and the next line with your code + for(let i=0; i < arr.length; i++){ + for (let j=0; j < arr[i].length; j++){ + product = product * arr[i][j]; + } + } console.log(arr, product); return product; } -const result = multiplyAll([[1, 2], [3, 4], [5, 6]]); -console.log(result); // 720 +const result = multiplyAll([[3, 4], [5, 6], [7, 8]]); +console.log(result); // 20160 // Do not change or remove anything below this line module.exports = multiplyAll; + diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js deleted file mode 100644 index dcd135040..000000000 --- a/Week3/homework/step2-2.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { - const numbers = []; - - // 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); -} - -function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); -} - -threeFive(10, 15, sayThree, sayFive); - -// Do not change or remove anything below this line -module.exports = threeFive; From 2e140a2556ae90b0188b937f20d74241571a3e21 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 12 Aug 2019 13:21:46 +0200 Subject: [PATCH 5/7] ex. 2-2, 2-3, 2-4, 2-5 --- Week3/homework/MY-index.html | 5 +++-- ...ep2-3.js => MY-step2-3-Repeat_a_String.js} | 22 +++++++++++++++---- ... MY-step2-4-Some_practice_with_objects.js} | 0 ...-loops.js => MY-step2-5-Nest_for_loops.js} | 0 4 files changed, 21 insertions(+), 6 deletions(-) rename Week3/homework/{step2-3.js => MY-step2-3-Repeat_a_String.js} (56%) rename Week3/homework/{MY-step2-4-Some-practice-with-objects.js => MY-step2-4-Some_practice_with_objects.js} (100%) rename Week3/homework/{MY-step2-5-Nest-for-loops.js => MY-step2-5-Nest_for_loops.js} (100%) diff --git a/Week3/homework/MY-index.html b/Week3/homework/MY-index.html index 1d441d38e..dfa34162a 100644 --- a/Week3/homework/MY-index.html +++ b/Week3/homework/MY-index.html @@ -9,8 +9,9 @@ - - + + + diff --git a/Week3/homework/step2-3.js b/Week3/homework/MY-step2-3-Repeat_a_String.js similarity index 56% rename from Week3/homework/step2-3.js rename to Week3/homework/MY-step2-3-Repeat_a_String.js index 00845c5eb..fda5d3619 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/MY-step2-3-Repeat_a_String.js @@ -1,30 +1,40 @@ '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 + for (let i = 0; i < num; i++) { + result += str; + } console.log(str, num, result); return result; } -console.log('for', repeatStringNumTimesWithFor('abc', 3)); +console.log('for', repeatStringNumTimesWithFor('abc', 2)); +/*-----------------------------------------------------------------------------------*/ // 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 + while (num > 0) { + result += str; + num--; + } console.log(str, num, result); return result; } -console.log('while', repeatStringNumTimesWithWhile('abc', 3)); +console.log('while', repeatStringNumTimesWithWhile('abc', 5)); + +/*-----------------------------------------------------------------------------------*/ // Use a 'do...while' loop function repeatStringNumTimesWithDoWhile(str, num) { @@ -32,13 +42,17 @@ function repeatStringNumTimesWithDoWhile(str, num) { let result = ''; // Replace this comment and the next line with your code + do { + result += str; + } while (result.length < str.length * num); console.log(str, num, result); return result; } -console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3)); +console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 7)); +/*-----------------------------------------------------------------------------------*/ // Do not change or remove anything below this line module.exports = { repeatStringNumTimesWithFor, diff --git a/Week3/homework/MY-step2-4-Some-practice-with-objects.js b/Week3/homework/MY-step2-4-Some_practice_with_objects.js similarity index 100% rename from Week3/homework/MY-step2-4-Some-practice-with-objects.js rename to Week3/homework/MY-step2-4-Some_practice_with_objects.js diff --git a/Week3/homework/MY-step2-5-Nest-for-loops.js b/Week3/homework/MY-step2-5-Nest_for_loops.js similarity index 100% rename from Week3/homework/MY-step2-5-Nest-for-loops.js rename to Week3/homework/MY-step2-5-Nest_for_loops.js From ae3cc7914b89f16d8b057c82e26a1992181e22c2 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 14 Aug 2019 14:31:46 +0200 Subject: [PATCH 6/7] polished up a bit --- Week3/homework/MY-index.html | 15 +++---- .../homework/MY-step2-6-Flatten_the_Array.js | 40 +++++++++++++++++++ .../homework/MY-step2-7-Different_Results.js | 27 +++++++++++++ ...ep3.js => MY-step3-Base_Scope_Closures.js} | 6 +++ .../{step3-bonus.js => MY-step3-bonus.js} | 0 Week3/homework/step2-6.js | 23 ----------- Week3/homework/step2-7.js | 23 ----------- 7 files changed, 81 insertions(+), 53 deletions(-) create mode 100644 Week3/homework/MY-step2-6-Flatten_the_Array.js create mode 100644 Week3/homework/MY-step2-7-Different_Results.js rename Week3/homework/{step3.js => MY-step3-Base_Scope_Closures.js} (84%) rename Week3/homework/{step3-bonus.js => MY-step3-bonus.js} (100%) delete mode 100644 Week3/homework/step2-6.js delete mode 100644 Week3/homework/step2-7.js diff --git a/Week3/homework/MY-index.html b/Week3/homework/MY-index.html index dfa34162a..0b7645325 100644 --- a/Week3/homework/MY-index.html +++ b/Week3/homework/MY-index.html @@ -7,12 +7,13 @@ Document - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/Week3/homework/MY-step2-6-Flatten_the_Array.js b/Week3/homework/MY-step2-6-Flatten_the_Array.js new file mode 100644 index 000000000..75e9917fc --- /dev/null +++ b/Week3/homework/MY-step2-6-Flatten_the_Array.js @@ -0,0 +1,40 @@ +'use strict'; + +const arr2d = [[1, 2], [3, 4], [5, 6]]; +const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; + +// const flattenArray2d = (arr) => arr.flat() +// const flattenArray3d = (arr) => arr.flat(2) //depth of 2 + +// const flattenArray2d = flatten +// const flattenArray3d = flatten + + +function flattenArray2d(arr) { + let flattenArr2 = []; + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + flattenArr2.push(arr[i][j]) + } + } + return flattenArr2; +} + +function flattenArray3d(arr) { + const flattenArr3 = []; + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + for (let l = 0; l < arr[i][j].length; l++) { + flattenArr3.push(arr[i][j][l]); + } + } + } + return flattenArr3; +} + + +//Do not change or remove anything below this line +module.exports = { + flattenArray2d, + flattenArray3d, +}; \ No newline at end of file diff --git a/Week3/homework/MY-step2-7-Different_Results.js b/Week3/homework/MY-step2-7-Different_Results.js new file mode 100644 index 000000000..b464e185a --- /dev/null +++ b/Week3/homework/MY-step2-7-Different_Results.js @@ -0,0 +1,27 @@ +'use strict'; + +const x = 9; +function f1(val) { + val = val + 1; + return val; +} + +f1(x); + +console.log(x); + +const y = { x: 9 }; +function f2(val) { + val.x = val.x + 1; + return val; +} + +f2(y); + +console.log(y); + +// Add your explanation as a comment here + +//x is 9 and does not change after the f1 function is executed but it does if I console.log(f1(x)), I do not know why. Maybe because x was defined outside of the f1 function scope + +//y is returned after going through the f2 function where the value of y(that is an object containing x) is changed by adding 1 to x. 9+1=10 diff --git a/Week3/homework/step3.js b/Week3/homework/MY-step3-Base_Scope_Closures.js similarity index 84% rename from Week3/homework/step3.js rename to Week3/homework/MY-step3-Base_Scope_Closures.js index 292724bf4..a47f42bcb 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/MY-step3-Base_Scope_Closures.js @@ -2,8 +2,14 @@ function createBase(base) { // Replace this comment and the next line with your code + + return function(number) { + return base + number + console.log(base); } +} + const addSix = createBase(6); diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/MY-step3-bonus.js similarity index 100% rename from Week3/homework/step3-bonus.js rename to Week3/homework/MY-step3-bonus.js diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js deleted file mode 100644 index ffe95b9f7..000000000 --- a/Week3/homework/step2-6.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -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); -} - -function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); -} - -console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 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, -}; diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js deleted file mode 100644 index 3e72e8551..000000000 --- a/Week3/homework/step2-7.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -const x = 9; -function f1(val) { - val = val + 1; - return val; -} - -f1(x); - -console.log(x); - -const y = { x: 9 }; -function f2(val) { - val.x = val.x + 1; - return val; -} - -f2(y); - -console.log(y); - -// Add your explanation as a comment here From e5d3c83baed1d4d594cc3a9a2ce97264c9c12a79 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 14 Aug 2019 16:47:15 +0200 Subject: [PATCH 7/7] fixed --- Week3/homework/MY-step2-1-Foo.js | 14 ++++++++++++++ Week3/homework/MY-step2-7-Different_Results.js | 5 ++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 Week3/homework/MY-step2-1-Foo.js diff --git a/Week3/homework/MY-step2-1-Foo.js b/Week3/homework/MY-step2-1-Foo.js new file mode 100644 index 000000000..0f58608d5 --- /dev/null +++ b/Week3/homework/MY-step2-1-Foo.js @@ -0,0 +1,14 @@ +'use strict'; + +function foo(func) { + func() + console.log(func); +} + +function bar() { + console.log('Hello, I am bar!'); +} +foo(bar); + +// Do not change or remove anything below this line +module.exports = foo; diff --git a/Week3/homework/MY-step2-7-Different_Results.js b/Week3/homework/MY-step2-7-Different_Results.js index b464e185a..f1b3596e1 100644 --- a/Week3/homework/MY-step2-7-Different_Results.js +++ b/Week3/homework/MY-step2-7-Different_Results.js @@ -22,6 +22,5 @@ console.log(y); // Add your explanation as a comment here -//x is 9 and does not change after the f1 function is executed but it does if I console.log(f1(x)), I do not know why. Maybe because x was defined outside of the f1 function scope - -//y is returned after going through the f2 function where the value of y(that is an object containing x) is changed by adding 1 to x. 9+1=10 +// in function f1 x is passed by value, therefore does not change +//in function f2 x is passed by reference, y is returned after going through the f2 function where the value of y(that is an object containing x) is changed by adding 1 to x. 9+1=10