diff --git a/Week3/homework/index.html b/Week3/homework/index.html new file mode 100644 index 000000000..ceb120f99 --- /dev/null +++ b/Week3/homework/index.html @@ -0,0 +1,20 @@ + + + + + + + Document + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..4ce66e7e1 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -3,6 +3,7 @@ function foo(func) { // What to do here? // Replace this comment and the next line with your code + func = "Nice to meet you, Bar." console.log(func); } @@ -10,7 +11,7 @@ function 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; diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..5727b9fb3 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -1,23 +1,31 @@ '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); + let numbers = []; + for (let i = startIndex; i <= stopIndex; i++) { + numbers.push(i); + } + + numbers.forEach(x => { + if (x % 3 === 0) threeCallback(x) + if (x % 5 === 0) fiveCallback(x) + }); +console.log(numbers) + } + + //console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers); function sayThree(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(`${number} is divisible by 3`) } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(`${number} is divisible by 5`) } threeFive(10, 15, sayThree, sayFive); // Do not change or remove anything below this line -module.exports = threeFive; +//module.exports = threeFive; diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index 00845c5eb..4f74351aa 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -5,8 +5,9 @@ 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 + " " + } return result; } @@ -17,22 +18,27 @@ console.log('for', repeatStringNumTimesWithFor('abc', 3)); 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); + let i = 0; + while (i < 3) { + result += str + ' '; + i++; + } return result; } console.log('while', repeatStringNumTimesWithWhile('abc', 3)); -// Use a 'do...while' loop +// // 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); + let i = 0; + do { + result += str + ' '; + i++; + } + while (i < num) return result; } @@ -40,8 +46,8 @@ function repeatStringNumTimesWithDoWhile(str, num) { console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3)); // Do not change or remove anything below this line -module.exports = { - repeatStringNumTimesWithFor, - repeatStringNumTimesWithWhile, - repeatStringNumTimesWithDoWhile, -}; +// module.exports = { +// repeatStringNumTimesWithFor, +// repeatStringNumTimesWithWhile, +// repeatStringNumTimesWithDoWhile, +// }; diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..9dc26835b 100644 --- a/Week3/homework/step2-4.js +++ b/Week3/homework/step2-4.js @@ -1,10 +1,13 @@ 'use strict'; function Dog() { - // add your code here + this.name = "Iroko"; + this.color = "White"; + 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; diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..b4b0246c3 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -4,14 +4,17 @@ 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++) { + //console.log(arr, product); + for (let j = 0; j < arr[i].length; j++){ + product = product * arr[i][j]; + } + + } return product; } - 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; diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index ffe95b9f7..32f1c7af3 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -4,20 +4,20 @@ 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.flat(); } function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + + return arr.flat(2); + //flat(depth) <--- how many layers } 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, -}; +// module.exports = { +// flattenArray2d, +// flattenArray3d, +// }; diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..1cc02cb7b 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -20,4 +20,12 @@ f2(y); console.log(y); -// Add your explanation as a comment here +/* +1. In the first function, the value of x was not affected because it was declared outside +of the function scope and if it's just console log it, it will come out as the same value, +despite being previously passed as an argument for the f1() function. + +2. In the second function, y is an object. By calling val.x, we are modifying the x property +of the y object (not the whole object), and therefore when we console log y, the result is +{x: 10} and not {x:9} +*/ diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..dff60ad20 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -3,12 +3,19 @@ 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); + let newArr = [... new Set(arr)]; + return newArr; + + /* Set objects are collections of values. You can iterate through the elements + of a set in insertion order. A value in the Set may ONLY OCCUR ONCE; it is unique + in the Set's collection. + + Because each value in the Set has to be unique, the value equality will be checked. + */ } const uniqueValues = makeUnique(values); console.log(uniqueValues); // Do not change or remove anything below this line -module.exports = makeUnique; +//module.exports = makeUnique; diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..fdbf9a22f 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/step3.js @@ -1,8 +1,10 @@ 'use strict'; -function createBase(base) { - // Replace this comment and the next line with your code - console.log(base); +function createBase(base) { + function addX(x) { + return x + base; + } + return addX; } const addSix = createBase(6); @@ -11,4 +13,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;