Skip to content

Commit a98bf1f

Browse files
author
beliy-a
committed
add solving for all tasks
1 parent 7b2c820 commit a98bf1f

File tree

8 files changed

+54
-33
lines changed

8 files changed

+54
-33
lines changed

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

13+
914
module.exports = { sum };

Exercises/2-for-of.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 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 result = 0;
5+
6+
for (const val of args) {
7+
result += val;
8+
}
9+
10+
return result;
711
};
812

913
module.exports = { sum };

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

914
module.exports = { sum };

Exercises/4-do-while.js

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

15+
916
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, val) => acc += val, 0);
4+
75

86
module.exports = { sum };

Exercises/6-matrix.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
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+
const arrOfNum = [];
5+
6+
for (let i = 0; i < matrix.length; i++) {
7+
const row = matrix[i];
8+
for (let j = 0; j < row.length; j++) {
9+
arrOfNum.push(Math.max.apply(null, row));
10+
}
11+
}
12+
13+
return Math.max.apply(null, arrOfNum);
714
};
815

916
module.exports = { max };

Exercises/7-ages.js

Lines changed: 8 additions & 13 deletions
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 personAge = {};
5+
6+
for (const person in persons) {
7+
const { died, born } = persons[person];
8+
personAge[person] = died - born;
9+
}
10+
11+
return personAge;
1712
};
1813

1914
module.exports = { ages };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
"eslint": "^7.17.0",
1212
"hpw": "^0.1.14"
1313
}
14-
}
14+
}

0 commit comments

Comments
 (0)