From 7a154ee933de4bf9d7bdfdc2f2c486cf8251f26c Mon Sep 17 00:00:00 2001 From: u61 Date: Mon, 21 Jan 2019 12:15:17 +0100 Subject: [PATCH 1/2] JS2 Week3 Homework Done --- .gitignore | 2 ++ Week3/homework/step2-1.js | 3 +-- Week3/homework/step2-2.js | 22 +++++++++++++++------- Week3/homework/step2-3.js | 19 +++++++++++++------ Week3/homework/step2-4.js | 4 +++- Week3/homework/step2-5.js | 7 +++++-- Week3/homework/step2-6.js | 16 ++++++++++++---- Week3/homework/step2-7.js | 6 +++++- Week3/homework/step3-bonus.js | 4 ++-- Week3/homework/step3.js | 6 ++++-- 10 files changed, 62 insertions(+), 27 deletions(-) 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..b964d0e6d 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -2,19 +2,27 @@ 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 i = startIndex; + for (i; i <= stopIndex; i++) { + if (i % 3 === 0 && i % 5 === 0) { + threeCallback(i); + fiveCallback(i); + } else if (i % 3 === 0) { + threeCallback(i); + } else if (i % 5 === 0) { + fiveCallback(i); + } + numbers.push(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 divide by 3'); } function sayFive(number) { - // Replace this comment and the next line with your code - console.log(number); + console.log(number, 'can divide by 5'); } threeFive(10, 15, sayThree, sayFive); diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index 5278d64f4..d05d24df5 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -5,8 +5,11 @@ 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); + if (num > 0) { + for (num; num > 0; num--) { + result += str; + } + } return result; } @@ -18,8 +21,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; } @@ -31,8 +36,10 @@ 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); + 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..e4da92128 100644 --- a/Week3/homework/step3-bonus.js +++ b/Week3/homework/step3-bonus.js @@ -3,8 +3,8 @@ 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); + const unique = arr.filter((x, y, z) => z.indexOf(x) === y); + return unique; } const uniqueValues = makeUnique(values); diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..410ab4891 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); + return function addSix(num) { + const sum = base + num; + return sum; + }; } const addSix = createBase(6); From 95848327c2733e2bdfe2b272843a1678847bba2a Mon Sep 17 00:00:00 2001 From: u61 Date: Wed, 23 Jan 2019 14:27:14 +0100 Subject: [PATCH 2/2] corrections with regard to Jim's review --- Week3/homework/step2-2.js | 19 +++++++++---------- Week3/homework/step2-3.js | 18 ++++++++---------- Week3/homework/step3-bonus.js | 3 +-- Week3/homework/step3.js | 3 +-- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index b964d0e6d..75ee426c7 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -2,27 +2,26 @@ function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { const numbers = []; - let i = startIndex; - for (i; i <= stopIndex; i++) { - if (i % 3 === 0 && i % 5 === 0) { - threeCallback(i); - fiveCallback(i); - } else if (i % 3 === 0) { + for (let i = startIndex; i <= stopIndex; i++) { + numbers.push(i); + } + for (let i = startIndex; i <= stopIndex; i++) { + if (i % 3 === 0) { threeCallback(i); - } else if (i % 5 === 0) { + } + if (i % 5 === 0) { fiveCallback(i); } - numbers.push(i); } console.log('numbers:', numbers); } function sayThree(number) { - console.log(number, 'can divide by 3'); + console.log(number, 'can be divided by 3'); } function sayFive(number) { - console.log(number, 'can divide by 5'); + 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 d05d24df5..3d41f68d9 100644 --- a/Week3/homework/step2-3.js +++ b/Week3/homework/step2-3.js @@ -5,10 +5,8 @@ function repeatStringNumTimesWithFor(str, num) { // eslint-disable-next-line let result = ''; - if (num > 0) { - for (num; num > 0; num--) { - result += str; - } + for (num; num > 0; num--) { + result += str; } return result; @@ -35,12 +33,12 @@ console.log('while', repeatStringNumTimesWithWhile('abc', 3)); function repeatStringNumTimesWithDoWhile(str, num) { // eslint-disable-next-line let result = ''; - - do { - result += str; - num--; - } while (num > 0); - + if (num > 0) { + do { + result += str; + num--; + } while (num > 0); + } return result; } diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index e4da92128..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) { - const unique = arr.filter((x, y, z) => z.indexOf(x) === y); - return unique; + 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 410ab4891..aec4296cf 100644 --- a/Week3/homework/step3.js +++ b/Week3/homework/step3.js @@ -2,8 +2,7 @@ function createBase(base) { return function addSix(num) { - const sum = base + num; - return sum; + return base + num; }; }