Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit e12f43c

Browse files
committed
HomeWork JS2 Week3 Completed
1 parent 29162c3 commit e12f43c

9 files changed

Lines changed: 69 additions & 29 deletions

File tree

‎Week3/homework/step2-1.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

33
function foo(func) {
4-
// What to do here?
5-
// Replace this comment and the next line with your code
6-
console.log(func);
4+
console.log('Hello, I am foo!');
5+
// The is a call to bar function.
6+
func();
77
}
88

99
function bar() {

‎Week3/homework/step2-2.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22

33
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
44
const numbers = [];
5+
for (let i = startIndex; i <= stopIndex; i++) {
6+
numbers.push(i);
7+
}
58

6-
// Replace this comment and the next line with your code
7-
console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers);
9+
for (let j = startIndex; j <= stopIndex; j++) {
10+
if (j % 3 === 0 && j % 5 === 0) {
11+
threeCallback(j);
12+
fiveCallback(j);
13+
} else if (j % 3 === 0) {
14+
threeCallback(j);
15+
} else if (j % 5 === 0) {
16+
fiveCallback(j);
17+
}
18+
}
819
}
920

1021
function sayThree(number) {
1122
// Replace this comment and the next line with your code
12-
console.log(number);
23+
console.log(number + ' is devisable by three');
1324
}
1425

1526
function sayFive(number) {
1627
// Replace this comment and the next line with your code
17-
console.log(number);
28+
console.log(number + ' is devisable by five');
1829
}
1930

2031
threeFive(10, 15, sayThree, sayFive);

‎Week3/homework/step2-3.js‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
function repeatStringNumTimesWithFor(str, num) {
55
// eslint-disable-next-line prefer-const
66
let result = '';
7-
8-
// Replace this comment and the next line with your code
9-
console.log(str, num, result);
10-
7+
for (let i = num; i > 0; i--) {
8+
result += str;
9+
}
1110
return result;
1211
}
1312

@@ -17,10 +16,10 @@ console.log('for', repeatStringNumTimesWithFor('abc', 3));
1716
function repeatStringNumTimesWithWhile(str, num) {
1817
// eslint-disable-next-line prefer-const
1918
let result = '';
20-
21-
// Replace this comment and the next line with your code
22-
console.log(str, num, result);
23-
19+
while (num > 0) {
20+
result += str;
21+
num--;
22+
}
2423
return result;
2524
}
2625

@@ -31,8 +30,13 @@ function repeatStringNumTimesWithDoWhile(str, num) {
3130
// eslint-disable-next-line prefer-const
3231
let result = '';
3332

34-
// Replace this comment and the next line with your code
35-
console.log(str, num, result);
33+
do {
34+
if (num > 0) {
35+
result += str;
36+
}
37+
38+
num--;
39+
} while (num > 0);
3640

3741
return result;
3842
}

‎Week3/homework/step2-4.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
function Dog() {
4-
// add your code here
4+
this.name = 'Cha';
5+
this.color = 'blue';
6+
this.numLegs = 2;
57
}
68

79
const hound = new Dog();

‎Week3/homework/step2-5.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ function multiplyAll(arr) {
44
// eslint-disable-next-line
55
let product = 1;
66

7-
// Replace this comment and the next line with your code
8-
console.log(arr, product);
7+
for (let i = 0; i < arr.length; i++) {
8+
for (let j = 0; j < arr[i].length; j++) {
9+
product = product * arr[i][j];
10+
}
11+
12+
return product;
13+
}
914

1015
return product;
1116
}

‎Week3/homework/step2-6.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ const arr2d = [[1, 2], [3, 4], [5, 6]];
44
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
55

66
function flattenArray2d(arr) {
7-
// Replace this comment and the next line with your code
8-
console.log(arr);
7+
const flattArr2 = [];
8+
for (let i = 0; i < arr.length; i++) {
9+
for (let j = 0; j < arr[i].length; j++) {
10+
flattArr2.push(arr[i][j]);
11+
}
12+
}
13+
return flattArr2;
914
}
1015

1116
function flattenArray3d(arr) {
12-
// Replace this comment and the next line with your code
13-
console.log(arr);
17+
const flattArr3 = [];
18+
for (let i = 0; i < arr.length; i++) {
19+
for (let j = 0; j < arr[i].length; j++) {
20+
for (let k = 0; k < arr[j].length; k++) {
21+
flattArr3.push(arr[i][j][k]);
22+
}
23+
}
24+
}
25+
return flattArr3;
1426
}
1527

1628
console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6]

‎Week3/homework/step2-7.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ f2(y);
2020

2121
console.log(y);
2222

23-
// Add your explanation as a comment here
23+
// The reason for printing different result here is the way the variables are used like
24+
// In first function the value 9 is passed by value so changing the value inside the function.
25+
// mean val parameter just copied the value of x and used in the function and did nothing to actual value.
26+
// 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.
27+
// 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
28+
// 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.

‎Week3/homework/step3-bonus.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
44

55
function makeUnique(arr) {
6-
// Replace this comment and the next line with your code
7-
console.log(arr);
6+
const result = arr.filter((i, j) => arr.indexOf(i) === j);
7+
return result;
88
}
99

1010
const uniqueValues = makeUnique(values);

‎Week3/homework/step3.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
function createBase(base) {
4-
// Replace this comment and the next line with your code
5-
console.log(base);
4+
return function(x) {
5+
return base + x;
6+
};
67
}
78

89
const addSix = createBase(6);

0 commit comments

Comments
 (0)