Skip to content

Commit 1ca9ca9

Browse files
committed
Labs 1-7
1 parent 2c13705 commit 1ca9ca9

File tree

7 files changed

+51
-32
lines changed

7 files changed

+51
-32
lines changed

Exercises/1-for.js

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

33
const sum = (...args) => {
4-
// Use for loop and accumulator variable
5-
// to calculate sum of all given arguments
6-
// For example sum(1, 2, 3) should return 6
4+
let res = 0;
5+
for (let i = 0; i < args.length; i++) {
6+
res += args[i];
7+
} return res;
78
};
89

910
module.exports = { sum };

Exercises/2-for-of.js

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

33
const sum = (...args) => {
4-
// Use for..of loop and accumulator variable
5-
// to calculate sum of all given arguments
6-
// For example sum(1, 2, 3) should return 6
4+
let res = 0;
5+
for (const value of args) {
6+
res += value;
7+
} return res;
78
};
89

910
module.exports = { sum };

Exercises/3-while.js

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

33
const sum = (...args) => {
4-
// Use while loop and accumulator variable
5-
// to calculate sum of all given arguments
6-
// For example sum(1, 2, 3) should return 6
4+
let result = 0;
5+
while (args.length > 0) {
6+
result += args.pop();
7+
} return result;
78
};
89

910
module.exports = { sum };

Exercises/4-do-while.js

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

33
const sum = (...args) => {
4-
// Use do..while loop and accumulator variable
5-
// to calculate sum of all given arguments
6-
// For example sum(1, 2, 3) should return 6
4+
if (args.length === 0) return 0;
5+
let result = 0;
6+
do {
7+
result += args.pop();
8+
} while (args.length > 0);
9+
return result;
710
};
811

912
module.exports = { sum };

Exercises/5-reduce.js

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

3-
const sum = (...args) => 0;
4-
// Use Array.prototype.reduce method
5-
// to calculate sum of all given arguments
6-
// For example sum(1, 2, 3) should return 6
3+
const sum = (...args) => args.reduce(((acc, cur) => acc + cur), 0);
74

85
module.exports = { sum };

Exercises/6-matrix.js

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

33
const max = matrix => {
4-
// Use nested for loop to find max value in 2d matrix
5-
// For example max([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
6-
// should return 9
4+
let value = matrix[0][0];
5+
for (const index in matrix) {
6+
const line = matrix[index];
7+
for (const index2 in line) {
8+
const colum = line[index2];
9+
if (colum > value) value = colum;
10+
}
11+
}
12+
return value;
713
};
814

15+
/*const max = matrix => {
16+
let value = matrix[0][0];
17+
for (const arr of matrix) {
18+
for (const i of arr) {
19+
if (i > value) value = i;
20+
}
21+
}
22+
return value;
23+
};*/
24+
25+
/*const max = matrix => {
26+
const modeMatrix = matrix
27+
.map(arr => arr.reduce((a, b) => (a > b ? a : b)))
28+
.reduce((a, b) => (a > b ? a : b));
29+
return modeMatrix;
30+
};*/
31+
932
module.exports = { max };

Exercises/7-ages.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
'use strict';
22

33
const ages = persons => {
4-
// Use for..in to calculate age for each person
5-
// For example ages({
6-
// lenin: { born: 1870, died: 1924 },
7-
// mao: { born: 1893, died: 1976 },
8-
// gandhi: { born: 1869, died: 1948 },
9-
// hirohito: { born: 1901, died: 1989 },
10-
// })
11-
// should return {
12-
// lenin: 54,
13-
// mao: 83,
14-
// gandhi: 79,
15-
// hirohito: 88,
16-
// }
4+
const age = {};
5+
for (const key in persons) {
6+
const person = persons[key];
7+
age[key] = person.died - person.born;
8+
}
9+
return age;
1710
};
1811

1912
module.exports = { ages };

0 commit comments

Comments
 (0)