Skip to content

Commit d07a62b

Browse files
authored
Ik-12 Chubko All labwork from 1 to 7
1 parent 5b83486 commit d07a62b

File tree

7 files changed

+50
-29
lines changed

7 files changed

+50
-29
lines changed

Exercises/1-for.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const sum = (...args) => {
44
// Use for loop and accumulator variable
55
// to calculate sum of all given arguments
66
// For example sum(1, 2, 3) should return 6
7+
let total = 0;
8+
for (let i = 0; i < args.length; i++) {
9+
total += args[i]; // Додаємо кожен аргумент до суми
10+
}
11+
return total;
712
};
813

914
module.exports = { sum };

Exercises/2-for-of.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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 total = 0;
5+
for (const arg of args) {
6+
total += arg;
7+
}
8+
return total;
79
};
810

911
module.exports = { sum };

Exercises/3-while.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
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 total = 0;
5+
let i = 0;
6+
7+
while (i < args.length) {
8+
total += args[i];
9+
i++;
10+
}
11+
12+
return total;
713
};
814

915
module.exports = { sum };

Exercises/4-do-while.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
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+
let total = 0;
5+
let i = 0;
6+
7+
do {
8+
total += args[i];
9+
i++;
10+
} while (i < args.length);
11+
12+
return total;
713
};
814

915
module.exports = { sum };

Exercises/5-reduce.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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) => {
4+
return args.reduce((total, current) => total + current, 0);
5+
};
76

87
module.exports = { sum };

Exercises/6-matrix.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
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 maxValue = -Infinity;
5+
6+
for (const row of matrix) {
7+
for (const value of row) {
8+
if (value > maxValue) {
9+
maxValue = value;
10+
}
11+
}
12+
}
13+
14+
return maxValue;
715
};
816

917
module.exports = { max };

Exercises/7-ages.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
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 ages = {};
5+
6+
for (const person in persons) {
7+
const personData = persons[person];
8+
ages[person] = personData.died - personData.born;
9+
}
10+
11+
return ages;
1712
};
1813

1914
module.exports = { ages };

0 commit comments

Comments
 (0)