Skip to content

Commit 1163f87

Browse files
author
Elena Kononchuk
committed
repo with tasks HowProgrammingWorks#4
1 parent a6d200c commit 1163f87

File tree

7 files changed

+47
-42
lines changed

7 files changed

+47
-42
lines changed

Exercises/1-for.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
'use strict';
1+
"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+
}
8+
return res;
79
};
810

911
module.exports = { sum };

Exercises/2-for-of.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
'use strict';
1+
"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 num of args) {
6+
res += num;
7+
}
8+
return res;
79
};
810

911
module.exports = { sum };

Exercises/3-while.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
'use strict';
1+
"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 res = 0;
5+
while (args.length > 0) {
6+
res += args.shift();
7+
}
8+
return res;
79
};
810

911
module.exports = { sum };

Exercises/4-do-while.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
'use strict';
1+
"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 res = 0;
6+
do {
7+
res += args.shift();
8+
} while (args.length > 0);
9+
return res;
710
};
811

912
module.exports = { sum };

Exercises/5-reduce.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
'use strict';
1+
"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, args) => acc + args, 0);
74

85
module.exports = { sum };

Exercises/6-matrix.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
'use strict';
1+
"use strict";
22

3-
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
3+
const max = (matrix) => {
4+
let arr2d = matrix[0][0];
5+
for (let i = 0; i < matrix.length; i++) {
6+
const row = matrix[i];
7+
for (let a = 0; a < row.length; a++) {
8+
const col = row[a];
9+
if (arr2d < col) arr2d = col;
10+
}
11+
}
12+
return arr2d;
713
};
814

915
module.exports = { max };

Exercises/7-ages.js

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

3-
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-
// }
17-
};
3+
const ages = (persons) => {
4+
const staf = {};
185

6+
for (const i in persons) {
7+
const info = persons[i];
8+
staf[i] = info.died - info.born;
9+
}
10+
return staf;
11+
};
1912
module.exports = { ages };

0 commit comments

Comments
 (0)