Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2e24e9

Browse files
committed
js2 homework for week3 submission
1 parent 90b74a5 commit b2e24e9

File tree

9 files changed

+60
-18
lines changed

9 files changed

+60
-18
lines changed

Week3/homework/step2-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function foo(func) {
44
// What to do here?
55
// Replace this comment and the next line with your code
6-
console.log(func);
6+
return func();
77
}
88

99
function bar() {

Week3/homework/step2-2.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
44
const numbers = [];
5-
65
// Replace this comment and the next line with your code
6+
for (let i = startIndex; i <= stopIndex; i++) {
7+
numbers.push(i);
8+
if (i % 15 === 0) {
9+
threeCallback(i);
10+
fiveCallback(i);
11+
} else if (i % 5 === 0) {
12+
fiveCallback(i);
13+
} else if (i % 3 === 0) {
14+
threeCallback(i);
15+
}
16+
}
717
console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers);
818
}
919

1020
function sayThree(number) {
1121
// Replace this comment and the next line with your code
12-
console.log(number);
22+
console.log(number + ' is divisible by 3');
1323
}
1424

1525
function sayFive(number) {
1626
// Replace this comment and the next line with your code
17-
console.log(number);
27+
console.log(number + ' is divisible by 5');
1828
}
1929

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

Week3/homework/step2-3.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ function repeatStringNumTimesWithFor(str, num) {
66
let result = '';
77

88
// Replace this comment and the next line with your code
9-
console.log(str, num, result);
10-
11-
return result;
9+
if (num > 0) {
10+
for (let i = 0; i < num; i++) {
11+
result += str;
12+
}
13+
return result;
14+
}
15+
return '';
1216
}
1317

1418
console.log('for', repeatStringNumTimesWithFor('abc', 3));
@@ -19,8 +23,10 @@ function repeatStringNumTimesWithWhile(str, num) {
1923
let result = '';
2024

2125
// Replace this comment and the next line with your code
22-
console.log(str, num, result);
23-
26+
while (num > 0) {
27+
result += str;
28+
num--;
29+
}
2430
return result;
2531
}
2632

@@ -32,8 +38,10 @@ function repeatStringNumTimesWithDoWhile(str, num) {
3238
let result = '';
3339

3440
// Replace this comment and the next line with your code
35-
console.log(str, num, result);
36-
41+
do {
42+
result += str;
43+
num--;
44+
} while (num > 0);
3745
return result;
3846
}
3947

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 = 'Johnny';
5+
this.color = 'whiite';
6+
this.numLegs = 4;
57
}
68

79
const hound = new Dog();

Week3/homework/step2-5.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ function multiplyAll(arr) {
55
let product = 1;
66

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

Week3/homework/step2-6.js

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

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

1117
function flattenArray3d(arr) {
1218
// Replace this comment and the next line with your code
13-
console.log(arr);
19+
const flattenedArr = [];
20+
for (let i = 0; i < arr.length; i++) {
21+
for (let j = 0; j < arr[i].length; j++) {
22+
for (let k = 0; k < arr[i][j].length; k++) {
23+
flattenedArr.push(arr[i][j][k]);
24+
}
25+
}
26+
}
27+
return flattenedArr;
1428
}
1529

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

Week3/homework/step2-7.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ f2(y);
2121
console.log(y);
2222

2323
// Add your explanation as a comment here
24+
// JavaScript is call by value.
25+
// However, in case if object, the value being passed is the reference.

Week3/homework/step3-bonus.js

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

55
function makeUnique(arr) {
66
// Replace this comment and the next line with your code
7-
console.log(arr);
7+
const uniqueArray = arr.filter((item, pos) => arr.indexOf(item) === pos);
8+
return uniqueArray;
89
}
910

1011
const uniqueValues = makeUnique(values);

Week3/homework/step3.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

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

810
const addSix = createBase(6);

0 commit comments

Comments
 (0)