From e12f43c5151a6485673c6de0f71016c605cf3bd8 Mon Sep 17 00:00:00 2001 From: usaghir Date: Sat, 23 Mar 2019 23:49:42 +0100 Subject: [PATCH] HomeWork JS2 Week3 Completed --- Week3/homework/step2-1.js | 6 +++--- Week3/homework/step2-2.js | 19 +++++++++++++++---- Week3/homework/step2-3.js | 24 ++++++++++++++---------- Week3/homework/step2-4.js | 4 +++- Week3/homework/step2-5.js | 9 +++++++-- Week3/homework/step2-6.js | 20 ++++++++++++++++---- Week3/homework/step2-7.js | 7 ++++++- Week3/homework/step3-bonus.js | 4 ++-- Week3/homework/step3.js | 5 +++-- 9 files changed, 69 insertions(+), 29 deletions(-) diff --git a/Week3/homework/step2-1.js b/Week3/homework/step2-1.js index d5699882c..dfb4aa187 100644 --- a/Week3/homework/step2-1.js +++ b/Week3/homework/step2-1.js @@ -1,9 +1,9 @@ 'use strict'; function foo(func) { - // What to do here? - // Replace this comment and the next line with your code - console.log(func); + console.log('Hello, I am foo!'); + // The is a call to bar function. + func(); } function bar() { diff --git a/Week3/homework/step2-2.js b/Week3/homework/step2-2.js index dcd135040..540190e11 100644 --- a/Week3/homework/step2-2.js +++ b/Week3/homework/step2-2.js @@ -2,19 +2,30 @@ function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) { const numbers = []; + for (let i = startIndex; i <= stopIndex; i++) { + numbers.push(i); + } - // Replace this comment and the next line with your code - console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers); + for (let j = startIndex; j <= stopIndex; j++) { + if (j % 3 === 0 && j % 5 === 0) { + threeCallback(j); + fiveCallback(j); + } else if (j % 3 === 0) { + threeCallback(j); + } else if (j % 5 === 0) { + fiveCallback(j); + } + } } function sayThree(number) { // Replace this comment and the next line with your code - console.log(number); + console.log(number + ' is devisable by three'); } function sayFive(number) { // Replace this comment and the next line with your code - console.log(number); + console.log(number + ' is devisable by five'); } threeFive(10, 15, sayThree, sayFive); diff --git a/Week3/homework/step2-3.js b/Week3/homework/step2-3.js index 00845c5eb..f7a900782 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 = num; i > 0; i--) { + result += str; + } return result; } @@ -17,10 +16,10 @@ 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); - + while (num > 0) { + result += str; + num--; + } return result; } @@ -31,8 +30,13 @@ 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); + do { + if (num > 0) { + result += str; + } + + num--; + } while (num > 0); return result; } diff --git a/Week3/homework/step2-4.js b/Week3/homework/step2-4.js index b11b1dcb6..76da5fd06 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 = 'Cha'; + this.color = 'blue'; + this.numLegs = 2; } const hound = new Dog(); diff --git a/Week3/homework/step2-5.js b/Week3/homework/step2-5.js index cbb54fa1d..11693b156 100644 --- a/Week3/homework/step2-5.js +++ b/Week3/homework/step2-5.js @@ -4,8 +4,13 @@ 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 = product * arr[i][j]; + } + + return product; + } return product; } diff --git a/Week3/homework/step2-6.js b/Week3/homework/step2-6.js index ffe95b9f7..09f165e5c 100644 --- a/Week3/homework/step2-6.js +++ b/Week3/homework/step2-6.js @@ -4,13 +4,25 @@ 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); + const flattArr2 = []; + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + flattArr2.push(arr[i][j]); + } + } + return flattArr2; } function flattenArray3d(arr) { - // Replace this comment and the next line with your code - console.log(arr); + const flattArr3 = []; + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + for (let k = 0; k < arr[j].length; k++) { + flattArr3.push(arr[i][j][k]); + } + } + } + return flattArr3; } console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6] diff --git a/Week3/homework/step2-7.js b/Week3/homework/step2-7.js index 3e72e8551..f27b95f45 100644 --- a/Week3/homework/step2-7.js +++ b/Week3/homework/step2-7.js @@ -20,4 +20,9 @@ f2(y); console.log(y); -// Add your explanation as a comment here +// The reason for printing different result here is the way the variables are used like +// In first function the value 9 is passed by value so changing the value inside the function. +// mean val parameter just copied the value of x and used in the function and did nothing to actual value. +// In 2nd function the the value to y is passed which is an object and objects are passed by reference so any change to val in the function will change y too. +// because in case of passed by reference val didn't copy the value of y but one can say that after passing by reference here val and y both have the same +// value {x:9} not copied value like passed by value and any change to one of these will have impact on other that is what caused the change in the value of y. diff --git a/Week3/homework/step3-bonus.js b/Week3/homework/step3-bonus.js index 917091d61..3f2ff981e 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 result = arr.filter((i, j) => arr.indexOf(i) === j); + return result; } const uniqueValues = makeUnique(values); diff --git a/Week3/homework/step3.js b/Week3/homework/step3.js index 292724bf4..fa8f1e8d2 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(x) { + return base + x; + }; } const addSix = createBase(6);