diff --git a/Week3/homework/MY-index.html b/Week3/homework/MY-index.html new file mode 100644 index 000000000..0b7645325 --- /dev/null +++ b/Week3/homework/MY-index.html @@ -0,0 +1,19 @@ + + + + + + + Document + + + + + + + + + + + + \ No newline at end of file diff --git a/Week3/homework/step2-1.js b/Week3/homework/MY-step2-1-Foo.js similarity index 70% rename from Week3/homework/step2-1.js rename to Week3/homework/MY-step2-1-Foo.js index d5699882c..0f58608d5 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/MY-step2-1-Foo.js @@ -1,15 +1,13 @@ 'use strict'; function foo(func) { - // What to do here? - // Replace this comment and the next line with your code + func() console.log(func); } function bar() { console.log('Hello, I am bar!'); } - foo(bar); // Do not change or remove anything below this line diff --git a/Week3/homework/MY-step2-1-Hello_I_am_bar.js b/Week3/homework/MY-step2-1-Hello_I_am_bar.js new file mode 100644 index 000000000..a6e0a422e --- /dev/null +++ b/Week3/homework/MY-step2-1-Hello_I_am_bar.js @@ -0,0 +1,23 @@ +'use strict'; +/*sample*/ +// function doIt() { +// console.log('I am done'); +// } +// setTimeout(doIt, 5000); + +//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 + 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 +module.exports = foo; 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-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/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/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/step2-7.js b/Week3/homework/MY-step2-7-Different_Results.js similarity index 50% rename from Week3/homework/step2-7.js rename to Week3/homework/MY-step2-7-Different_Results.js index 3e72e8551..f1b3596e1 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/MY-step2-7-Different_Results.js @@ -21,3 +21,6 @@ f2(y); console.log(y); // Add your explanation as a comment here + +// 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 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-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; 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, -};