Skip to content

Commit 75fea09

Browse files
committed
HomeWork Iteration
1 parent bdada5d commit 75fea09

File tree

7 files changed

+47
-32
lines changed

7 files changed

+47
-32
lines changed

Exercises/1-for.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 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 total = 0;
5+
6+
for (let i = 0; i < args.length; i++) {
7+
total += args[i];
8+
}
9+
return total;
710
};
811

912
module.exports = { sum };

Exercises/2-for-of.js

Lines changed: 5 additions & 3 deletions
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 number of args) {
6+
total += number;
7+
}
8+
return total;
79
};
810

911
module.exports = { sum };

Exercises/3-while.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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+
while (i < args.length) {
7+
total += args[i];
8+
i++;
9+
}
10+
return total;
711
};
812

913
module.exports = { sum };

Exercises/4-do-while.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
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] || 0;
9+
i++;
10+
} while (i < args.length);
11+
return total;
712
};
813

914
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, num) => acc + num, 0);
74

85
module.exports = { sum };

Exercises/6-matrix.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
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+
const findMax = (arr) => {
7+
for (const el of arr) {
8+
if (Array.isArray(el)) {
9+
findMax(el);
10+
} else {
11+
maxValue = Math.max(maxValue, el);
12+
}
13+
}
14+
};
15+
16+
findMax(matrix);
17+
return maxValue;
718
};
819

20+
921
module.exports = { max };

Exercises/7-ages.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
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 result = {};
5+
for (const person in persons) {
6+
result[person] = persons[person].died - persons[person].born;
7+
}
8+
return result;
179
};
1810

1911
module.exports = { ages };

0 commit comments

Comments
 (0)