Skip to content

Commit 4d3d5b6

Browse files
committed
added requested changes
1 parent 26c00cc commit 4d3d5b6

8 files changed

Lines changed: 88 additions & 107 deletions

File tree

Week3/homework/2-step3.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
"use strict";
2-
function sayThree(x) {
3-
console.log(x + " is divisible by 3 ");
4-
}
5-
function sayFive(x) {
6-
console.log(x + " is divisible by 5 ");
7-
}
1+
'use strict';
82

93
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
10-
const values = [];
4+
const numbers = [];
115
for (let i = startIndex; i <= stopIndex; i++) {
12-
values.push(i);
6+
numbers.push(i);
137
}
14-
console.log(values);
15-
for (let i = 0; i < values.length; i++) {
16-
if (values[i] % 3 === 0 && values[i] % 5=== 0 ) {
17-
threeCallback(values[i]);
18-
fiveCallback(values[i]);
19-
}
20-
else if (values[i] % 3 === 0) {
21-
threeCallback(values[i]);
8+
9+
numbers.forEach(num => {
10+
if (num % 3 === 0) {
11+
threeCallback(num);
2212
}
23-
else if (values[i] % 5 === 0) {
24-
fiveCallback(values[i]);
13+
if (num % 5 === 0) {
14+
fiveCallback(num);
2515
}
26-
}
16+
});
2717
}
28-
threeFive(10, 15, sayThree, sayFive);
18+
19+
// uses a closure
20+
function sayDivisibleBy(divisor) {
21+
return function (num) {
22+
console.log(`${num} is divisible by ${divisor}`);
23+
};
24+
}
25+
26+
threeFive(10, 15, sayDivisibleBy(3), sayDivisibleBy(5));
27+

Week3/homework/3-step3.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1-
"use strict";
1+
'use strict';
22

33
// use a 'for' loop
44
function repeatStringNumTimesWithFor(str, num) {
5-
if (num <= 0) return "";
6-
else {
7-
let result = "";
8-
for (let i = 0; i < num; i++) {
9-
result += str;
10-
}
11-
return result;
5+
if (typeof num !== 'number' || num < 1) {
6+
return '';
127
}
8+
const origStr = str;
9+
for (let i = 1; i < num; i++) {
10+
str += origStr;
11+
}
12+
return str;
1313
}
1414

15-
repeatStringNumTimesWithFor("abc", 3);
1615

17-
console.log("for", repeatStringNumTimesWithFor("abc", 3));
16+
console.log('for', repeatStringNumTimesWithFor('abc', 3));
1817

1918
// use a 'while' loop
2019
function repeatStringNumTimesWithWhile(str, num) {
21-
if (num <= 0) {
22-
return "";
23-
} else {
24-
let repeatedStr = "";
25-
let i = 0;
26-
while (i < num) {
27-
repeatedStr += str;
28-
i++;
29-
}
30-
return repeatedStr;
20+
if (typeof num !== 'number' || num < 1) {
21+
return '';
22+
}
23+
const origStr = str;
24+
let i = 1;
25+
while (i < num) {
26+
str += origStr;
27+
i++;
3128
}
29+
return str;
3230
}
33-
console.log("while", repeatStringNumTimesWithWhile("abc", 3));
31+
32+
33+
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
3434

3535
// use a 'do...while' loop
3636
function repeatStringNumTimesWithDoWhile(str, num) {
37-
if (num <= 0) {
38-
return "";
39-
} else {
40-
let repeatedStr = "";
41-
let i = 0;
42-
do {
43-
i++;
44-
repeatedStr += str;
45-
} while (i < num);
46-
return repeatedStr;
37+
if (typeof num !== 'number' || num < 1) {
38+
return '';
4739
}
40+
const origStr = str;
41+
let i = 1;
42+
do {
43+
str += origStr;
44+
i++;
45+
} while (i < num);
46+
return str;
4847
}
49-
console.log("while", repeatStringNumTimesWithDoWhile("abc", 3));
48+
49+
50+
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3));

Week3/homework/4-step3.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict';
2-
function Dog() {
3-
this.name = "Rupert";
4-
this.color = "brown";
5-
this.numLegs = 4;
6-
}
72

8-
let hound = new Dog();
9-
////////////////
10-
function Dog() {
11-
this.name = "Jackie";
12-
this.color = "White";
13-
this.numLegs = 4;
14-
}
3+
var Car = function () {
4+
this.wheels = 4;
5+
this.engines = 1;
6+
this.seats = 5;
7+
};
8+
9+
var MotorBike = function () {
10+
this.wheels = 2;
11+
this.engines = 1;
12+
this.seats = 2;
13+
};
14+
15+
console.log(new Car());
16+
console.log(new MotorBike());

Week3/homework/5-step3.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
'use strict';
2+
23
function multiplyAll(arr) {
34
var product = 1;
45
for (var i = 0; i < arr.length; i++) {
5-
for (var j = 0; j < arr[i].length; j++) {
6-
console.log(arr[i][j]);
7-
product = product * arr[i][j];
8-
console.log(product * arr[i][j]);
6+
var innerArray = arr[i];
7+
for (var j = 0; j < innerArray.length; j++) {
8+
product *= innerArray[j];
99
}
1010
}
1111
return product;
1212
}
1313

14-
// Modify values below to test your code
15-
multiplyAll([[1], [2], [3]]);
16-
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
17-
multiplyAll([[5, 1], [0, 2, 4, 0.5], [3, 9]]);
14+
const result = multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
15+
console.log(result);

Week3/homework/6-step3.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
"use strict";
1+
'use strict';
22

33
const arr2d = [[1, 2], [3, 4], [5, 6]];
44
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
55

6-
for (let i = 0; i < arr3d.length; i++) {
7-
for (let j = 0; j < arr3d[i].length; j++) {
8-
for (let k = 0; k < arr3d[i][j].length; k++) {
9-
10-
}
6+
// This is a recursive function: a function calling itself.
7+
// It can print an array of any dimension.
8+
function printAll(arg) {
9+
if (Array.isArray(arg)) {
10+
arg.forEach(elem => printAll(elem));
11+
} else {
12+
console.log(arg);
1113
}
1214
}
1315

14-
// With for of
15-
16-
for (let item of arr3d) {
17-
console.log(item);
18-
}
19-
16+
console.log('\narr2d');
17+
printAll(arr2d);
2018

19+
console.log('\narr3d');
20+
printAll(arr3d);

Week3/homework/7-step3.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ function f1(val) {
99
f1(x);
1010

1111
console.log(x);
12-
/* Here we have created a variable and assigned it to a value x 9. Then we created a function
13-
and we pass a value to it, so the result should be 10, but here we just returned the
14-
value of constant x that has value 10. */
15-
const y = f1(x);
16-
console.log(y);
17-
12+
console.log('function f1 does not change the value of variable x: x is passed by value');
1813

1914

2015
const y = { x: 9 };
@@ -26,7 +21,6 @@ function f2(val) {
2621
f2(y);
2722

2823
console.log(y);
29-
/* Here we have an object and we have changed the value of the property of X 9
30-
to value X 10, so we can change the property even if the object is constant.
31-
*/
24+
console.log('function f2 changes the value of property y.x: y is passed by reference');
25+
3226

Week3/homework/step4-bonus.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,4 @@
22

33
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
44

5-
function uniqueValues() {
6-
let newArr = Array.from(new Set(values));
7-
return newArr;
8-
}
9-
10-
/* Second way */
115
let uniqueValues = [... new Set(values)];

Week3/homework/step4.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,3 @@ function createBase(num) {
99
const addSix = createBase(6);
1010
addSix(10); // returns 16
1111
addSix(21); // returns 27
12-
13-
/* Second way to structuring the Function*/
14-
createBase = base => console.log(base + num);
15-
const addSix = createBase(6);
16-
17-
18-

0 commit comments

Comments
 (0)