Skip to content

Commit d67a8d0

Browse files
Good solution for matrix(6-matrix.js)
1 parent dbaa3a5 commit d67a8d0

File tree

7 files changed

+42
-32
lines changed

7 files changed

+42
-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 res = 0;
5+
if (args) {
6+
for (let i = 0; i < args.length; i++) {
7+
res += args[i];
8+
}}
9+
return res;
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 res = 0;
5+
if (args) {
6+
for (const value of args) res += value;
7+
}
8+
return res;
79
};
810

911
module.exports = { sum };

Exercises/3-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 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+
if (args) {
6+
let i = 0
7+
while (i < args.length) res += args[i++];
8+
}
9+
return res;
710
};
811

912
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 res = 0;
5+
if (args[0]) { let i = 0;
6+
do {
7+
res += args[i];
8+
i = i + 1;
9+
} while (i < args.length);
10+
};
11+
return res;
712
};
813

914
module.exports = { sum };

Exercises/5-reduce.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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, x) => acc + x, 0);
4+
75

86
module.exports = { sum };

Exercises/6-matrix.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
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 list = [];
5+
for (let elem of matrix) {
6+
for (let inside of elem) {
7+
list.push(inside);
8+
}
9+
}
10+
// easy gggggggggggggggggggggggggggggggg
11+
return list.reduce((acc, x) => acc > x ? acc : x);
712
};
813

14+
915
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 res = {};
5+
for (const key in persons) {
6+
// value = date of death - date of birth
7+
res[key] = persons[key].died - persons[key].born;
8+
}
9+
return res;
1710
};
1811

1912
module.exports = { ages };

0 commit comments

Comments
 (0)