Skip to content

Commit c0ff63d

Browse files
Lab4 Iteration
1 parent a6d200c commit c0ff63d

10 files changed

+1327
-45
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"linebreak-style": [
2020
"error",
21-
"unix"
21+
"windows"
2222
],
2323
"quotes": [
2424
"error",

Exercises/1-for.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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 sum = 0;
5+
for (let i = 0; i < args.length; i++) {
6+
sum += args[i];
7+
}
8+
return sum;
79
};
8-
10+
console.log(sum(1, 2, 3));
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 @@
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 sum = 0;
5+
for (const element of args) {
6+
sum += element;
7+
}
8+
return sum;
79
};
8-
10+
console.log(sum());
911
module.exports = { sum };

Exercises/3-while.js

Lines changed: 7 additions & 4 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 sum = 0;
5+
let i = 0;
6+
while (i < args.length) {
7+
sum += args[i++];
8+
}
9+
return sum;
710
};
8-
11+
console.log(sum(1, 3));
912
module.exports = { sum };

Exercises/4-do-while.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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 === 0) return 0;
5+
let sum = 0;
6+
let i = 0;
7+
do {
8+
sum += args[i++];
9+
} while (i < args.length);
10+
return sum;
711
};
8-
12+
console.log(sum(1, 2, 3, 6));
913
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 @@
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
7-
3+
const sum = (...args) => args.reduce((prev, cur) => prev + cur, 0);
4+
console.log(sum(0, 3, -5));
85
module.exports = { sum };

Exercises/6-matrix.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
'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 max = matrix[0][0];
5+
for (let i = 0; i < matrix.length; i++) {
6+
const row = matrix[i];
7+
for (let j = 0; j < row.length; j++) {
8+
if (max < row[j])
9+
max = row[j];
10+
}
11+
}
12+
return max;
713
};
8-
14+
const m = max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
15+
console.log(m);
916
module.exports = { max };

Exercises/7-ages.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
'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-
// }
3+
const ages = (persons) => {
4+
const ages = {};
5+
for (const personsKey in persons) {
6+
ages[personsKey] = persons[personsKey].died - persons[personsKey].born;
7+
}
8+
return ages;
179
};
18-
10+
const persons = {
11+
lenin: { born: 1870, died: 1924 },
12+
mao: { born: 1893, died: 1976 },
13+
gandhi: { born: 1869, died: 1948 },
14+
hirohito: { born: 1901, died: 1989 },
15+
};
16+
console.log(ages(persons));
1917
module.exports = { ages };

0 commit comments

Comments
 (0)