diff --git a/.gitignore b/.gitignore index 6c589c2f8..f3f2f9494 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,5 @@ typings/ .netlify dist/ +exercise +live diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..dfb470867 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -2,8 +2,7 @@ function foo(func) { // What to do here? - // Replace this comment and the next line with your code - console.log(func); + func(); } function bar() { diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..75ee426c7 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -2,19 +2,26 @@ 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); + for (let i = startIndex; i <= stopIndex; i++) { + numbers.push(i); + } + for (let i = startIndex; i <= stopIndex; i++) { + if (i % 3 === 0) { + threeCallback(i); + } + if (i % 5 === 0) { + fiveCallback(i); + } + } + console.log('numbers:', numbers); } function sayThree(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(number, 'can be divided by 3'); } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(number, 'can be divided by 5'); } threeFive(10, 15, sayThree, sayFive); diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index 5278d64f4..3d41f68d9 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 let result = ''; - // Replace this comment and the next line with your code - console.log(str, num, result); + for (num; num > 0; num--) { + result += str; + } return result; } @@ -18,8 +19,10 @@ function repeatStringNumTimesWithWhile(str, num) { // eslint-disable-next-line 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; } @@ -30,10 +33,12 @@ console.log('while', repeatStringNumTimesWithWhile('abc', 3)); function repeatStringNumTimesWithDoWhile(str, num) { // eslint-disable-next-line let result = ''; - - // Replace this comment and the next line with your code - console.log(str, num, result); - + if (num > 0) { + do { + result += str; + num--; + } while (num > 0); + } return result; } diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..ed9ec75a4 100644 --- a/Week3/homework/step2-4.js +++ b/Week3/homework/step2-4.js @@ -1,7 +1,9 @@ 'use strict'; function Dog() { - // add your code here + this.name = 'rocky'; + this.color = 'red'; + this.numLegs = 5; } const hound = new Dog(); diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..6367554ac 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -4,8 +4,11 @@ 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++) { + for (let j = 0; j < arr[i].length; j++) { + product = arr[i][j] * product; + } + } return product; } diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index 76e458e4c..0d9da0fef 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -4,13 +4,21 @@ const arr2d = [[1, 2], [3, 4], [5, 6]]; const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; function printArray2d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + console.log(arr[i][j]); + } + } } function printArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + for (let k = 0; k < arr[i][j].length; k++) { + console.log(arr[i][j][k]); + } + } + } } printArray2d(arr2d); diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..8b86c0376 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -20,4 +20,8 @@ f2(y); console.log(y); -// Add your explanation as a comment here +// Pass by value rule: if you use a variable inside a function and change its value +// it doesn't affect variable's value which is outside of the function. + +// Pass by reference rule: if you use a variable's address or reference inside a function +// and change them it affects variable's value which is outside of the function. diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..0ccb6912a 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -3,8 +3,7 @@ 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); + return arr.filter((x, y, z) => z.indexOf(x) === y); } const uniqueValues = makeUnique(values); diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..aec4296cf 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/step3.js @@ -1,8 +1,9 @@ 'use strict'; function createBase(base) { - // Replace this comment and the next line with your code - console.log(base); + return function addSix(num) { + return base + num; + }; } const addSix = createBase(6);