Skip to content

Commit e05eea3

Browse files
step 2 and 3 added
1 parent 085ce28 commit e05eea3

9 files changed

Lines changed: 82 additions & 55 deletions

File tree

Week3/homework/step2-1.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
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+
return func();
75
}
86

97
function bar() {
10-
console.log('Hello, I am bar!');
8+
console.log('Hello, I am bar!');
119
}
1210

1311
foo(bar);

Week3/homework/step2-2.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
'use strict';
22

33
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
4-
const numbers = [];
4+
const numbers = [];
55

6-
// Replace this comment and the next line with your code
7-
console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers);
6+
for (let i = startIndex; i <= stopIndex; i++) {
7+
numbers.push(i);
8+
if (i % 3 === 0) {
9+
return threeCallback(numbers);
10+
} if (i % 5 === 0) {
11+
return fiveCallback(numbers);
12+
} if (i % 3 === 0 && i % 5 === 0) {
13+
threeCallback(numbers);
14+
fiveCallback(numbers);
15+
}
16+
}
817
}
918

1019
function sayThree(number) {
11-
// Replace this comment and the next line with your code
12-
console.log(number);
20+
// Replace this comment and the next line with your code
21+
console.log(`This ${number} is divisible by 3`);
1322
}
1423

1524
function sayFive(number) {
16-
// Replace this comment and the next line with your code
17-
console.log(number);
25+
// Replace this comment and the next line with your code
26+
console.log(`This ${number} is divisible by 5`);
1827
}
1928

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

Week3/homework/step2-3.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,47 @@
22

33
// Use a 'for' loop
44
function repeatStringNumTimesWithFor(str, num) {
5-
// eslint-disable-next-line prefer-const
6-
let result = '';
7-
8-
// Replace this comment and the next line with your code
9-
console.log(str, num, result);
10-
11-
return result;
5+
// eslint-disable-next-line prefer-const
6+
let result = " ";
7+
for (let i = 0; num > i; i++) {
8+
result += str;
9+
}
10+
return result;
1211
}
1312

13+
1414
console.log('for', repeatStringNumTimesWithFor('abc', 3));
1515

1616
// Use a 'while' loop
1717
function repeatStringNumTimesWithWhile(str, num) {
18-
// eslint-disable-next-line prefer-const
19-
let result = '';
20-
21-
// Replace this comment and the next line with your code
22-
console.log(str, num, result);
23-
24-
return result;
18+
// eslint-disable-next-line prefer-const
19+
let result = '';
20+
while (num > 0) {
21+
result += str;
22+
num--;
23+
}
24+
return result;
2525
}
2626

2727
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
2828

2929
// Use a 'do...while' loop
3030
function repeatStringNumTimesWithDoWhile(str, num) {
31-
// eslint-disable-next-line prefer-const
32-
let result = '';
33-
34-
// Replace this comment and the next line with your code
35-
console.log(str, num, result);
36-
37-
return result;
31+
// eslint-disable-next-line prefer-const
32+
let result = '';
33+
do {
34+
result += str;
35+
num--;
36+
} while (num > 0);
37+
38+
return result;
3839
}
3940

4041
console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3));
4142

4243
// Do not change or remove anything below this line
4344
module.exports = {
44-
repeatStringNumTimesWithFor,
45-
repeatStringNumTimesWithWhile,
46-
repeatStringNumTimesWithDoWhile,
45+
repeatStringNumTimesWithFor,
46+
repeatStringNumTimesWithWhile,
47+
repeatStringNumTimesWithDoWhile,
4748
};

Week3/homework/step2-4.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
2+
//constructor
23

34
function Dog() {
4-
// add your code here
5+
this.name = "Bobby";
6+
this.color = "Black";
7+
this.numLegs = 4;
58
}
69

710
const hound = new Dog();

Week3/homework/step2-5.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use strict';
22

33
function multiplyAll(arr) {
4-
// eslint-disable-next-line
5-
let product = 1;
4+
// eslint-disable-next-line
5+
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+
}
912

10-
return product;
13+
return product;
1114
}
1215

1316
const result = multiplyAll([[1, 2], [3, 4], [5, 6]]);

Week3/homework/step2-6.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,30 @@ 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+
let arr2Flat = [].concat.apply([], arr2d);
8+
console.log(arr2Flat);
99
}
1010

1111
function flattenArray3d(arr) {
12-
// Replace this comment and the next line with your code
13-
console.log(arr);
12+
const newArr3 = [];
13+
// let arr3Flat = [].concat(...arr3d); Why is this not working?
14+
for (let i = 0; i < arr3d.length; i++) {
15+
for (let j = 0; j < arr3d[i].length; j++) {
16+
for (let k = 0; k < arr3d[i][j].length; k++) {
17+
newArr3.push(arr3d[i][j][k]);
18+
19+
}
20+
}
21+
}
22+
return newArr3;
23+
1424
}
1525

1626
console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6]
1727
console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8]
1828

1929
// Do not change or remove anything below this line
2030
module.exports = {
21-
flattenArray2d,
22-
flattenArray3d,
31+
flattenArray2d,
32+
flattenArray3d,
2333
};

Week3/homework/step2-7.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const x = 9;
44
function f1(val) {
5-
val = val + 1;
6-
return val;
5+
val = val + 1;
6+
return val;
77
}
88

99
f1(x);
@@ -12,12 +12,13 @@ console.log(x);
1212

1313
const y = { x: 9 };
1414
function f2(val) {
15-
val.x = val.x + 1;
16-
return val;
15+
val.x = val.x + 1;
16+
return val;
1717
}
1818

1919
f2(y);
2020

2121
console.log(y);
2222

2323
// Add your explanation as a comment here
24+
//

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 uniqueValues = values.filter((x, i, a) => a.indexOf(x) == i);
7+
console.log(uniqueValues);
88
}
99

1010
const uniqueValues = makeUnique(values);

Week3/homework/step3.js

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

33
function createBase(base) {
4-
// Replace this comment and the next line with your code
5-
console.log(base);
4+
let sum6 = function (add6) {
5+
return base + add6;
6+
};
7+
return sum6;
68
}
79

810
const addSix = createBase(6);

0 commit comments

Comments
 (0)