diff --git a/Week3/homework/index.html b/Week3/homework/index.html new file mode 100644 index 000000000..fd0e8ace8 --- /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..2eb02309e 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -1,16 +1,14 @@ 'use strict'; function foo(func) { - // What to do here? - // Replace this comment and the next line with your code - console.log(func); + console.log('Hi bar!'); } function bar() { console.log('Hello, I am bar!'); } -foo(bar); +foo(bar()); // Do not change or remove anything below this line module.exports = foo; diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..a74694466 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -1,23 +1,24 @@ 'use strict'; function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { - const numbers = []; + const numbers = [...Array(stopIndex - startIndex + 1).keys()].map(x => x + startIndex); + console.log(numbers); - // Replace this comment and the next line with your code - console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers); + numbers.forEach(number => { + if (number % 3 === 0) threeCallback(number); + if (number % 5 === 0) fiveCallback(number); + }); } 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); +threeFive(9, 22, 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..10431f598 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -4,10 +4,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,9 +16,12 @@ 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 < num) { + result += str; + i++; + } return result; } @@ -30,9 +32,13 @@ console.log('while', repeatStringNumTimesWithWhile('abc', 3)); function repeatStringNumTimesWithDoWhile(str, num) { // eslint-disable-next-line prefer-const let result = ''; + let i = 0; - // Replace this comment and the next line with your code - console.log(str, num, result); + do { + result += str + ' '; + i++; + } + while (i < num); return result; } diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..ef4699575 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 = "Firulais"; + this.color = "black"; + this.numLegs = 4; } const hound = new Dog(); +console.log(hound); // Do not change or remove anything below this line module.exports = hound; diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..01e434422 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -4,14 +4,18 @@ 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++){ + const subArray = arr[i]; + for (let y = 0; y < subArray.length; y++){ + product *= arr[i][y]; + console.log(arr, product) + } + } return product; } const result = multiplyAll([[1, 2], [3, 4], [5, 6]]); -console.log(result); // 720 +console.log(result); // Do not change or remove anything below this line module.exports = multiplyAll; diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index ffe95b9f7..b5b4248b6 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -3,14 +3,12 @@ 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 flattenArray2d(arr){ + return arr.flat(); } function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + return arr.flat(2); } console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6] @@ -20,4 +18,4 @@ console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8] module.exports = { flattenArray2d, flattenArray3d, -}; +}; \ No newline at end of file diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..580430662 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -6,8 +6,6 @@ function f1(val) { return val; } -f1(x); - console.log(x); const y = { x: 9 }; @@ -21,3 +19,7 @@ f2(y); console.log(y); // Add your explanation as a comment here +// The variable in the first function is a number, while the variable in the second function is an object. +// f1 doesn't call the value of x, that's why when we console.log x, we get the value of that constant (9) +// f2 calls the value of x and makes an operation, thus resulting in a new object "{x :10}" + diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..7012bfdb6 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -1,14 +1,30 @@ '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); + let newArr = []; + for (let i = 0; i < arr.length; i++){ + if (newArr.indexOf(arr[i]) === -1) { + newArr.push(arr[i]); + } + } + return newArr; } +const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c']; +const names = ['lisa', 'lisa', 'priscilla', 'tania', 'priscilla', 'nadia', 'nadia', 'nadia']; +const numbers = [22, 22, 34, 30, 30, 30, 22, 15, 12, 29, 29, 28, 29]; + + const uniqueValues = makeUnique(values); -console.log(uniqueValues); +const uniqueNames = makeUnique(names); +const uniqueNumbers = makeUnique(numbers); + +console.log(uniqueValues, uniqueNames, uniqueNumbers); + + +// Another SHORTER option is: +// let newArr = [... new Set(arr)]; +// return newArr; // 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..dfbd7fad4 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 basePlusNum(num) { + return base + num; + } + return basePlusNum; } const addSix = createBase(6);