Skip to content

Commit 67bb991

Browse files
committed
All Labs Done
1 parent 2c13705 commit 67bb991

File tree

7 files changed

+79
-31
lines changed

7 files changed

+79
-31
lines changed

Exercises/1-for.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 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
};
810

911
module.exports = { sum };
12+
13+
// Use for loop and accumulator variable
14+
// to calculate sum of all given arguments
15+
// For example sum(1, 2, 3) should return 6

Exercises/2-for-of.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 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 arg of args) {
6+
sum += arg;
7+
}
8+
return sum;
79
};
810

911
module.exports = { sum };
12+
13+
// Use for..of loop and accumulator variable
14+
// to calculate sum of all given arguments
15+
// For example sum(1, 2, 3) should return 6

Exercises/3-while.js

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

913
module.exports = { sum };
14+
15+
// Use while loop and accumulator variable
16+
// to calculate sum of all given arguments
17+
// For example sum(1, 2, 3) should return 6

Exercises/4-do-while.js

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

914
module.exports = { sum };
15+
16+
// Use do..while loop and accumulator variable
17+
// to calculate sum of all given arguments
18+
// For example sum(1, 2, 3) should return 6

Exercises/5-reduce.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3-
const sum = (...args) => 0;
3+
const sum = (...args) => args.reduce((s, c) => s += c, 0);
4+
5+
module.exports = { sum };
6+
47
// Use Array.prototype.reduce method
58
// to calculate sum of all given arguments
69
// For example sum(1, 2, 3) should return 6
7-
8-
module.exports = { sum };

Exercises/6-matrix.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
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 max = 0;
5+
for (const array of matrix) {
6+
for (const item of array) {
7+
if (item > max) {
8+
max = item;
9+
}
10+
}
11+
}
12+
return max;
713
};
814

915
module.exports = { max };
16+
17+
// Use nested for loop to find max value in 2d matrix
18+
// For example max([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
19+
// should return 9

Exercises/7-ages.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
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 ages = {};
5+
for (const name in persons) {
6+
const person = persons[name];
7+
ages[name] = person.died - person.born;
8+
console.log(ages);
9+
}
10+
return ages;
1711
};
1812

1913
module.exports = { ages };
14+
15+
// Use for..in to calculate age for each person
16+
// For example ages({
17+
// lenin: { born: 1870, died: 1924 },
18+
// mao: { born: 1893, died: 1976 },
19+
// gandhi: { born: 1869, died: 1948 },
20+
// hirohito: { born: 1901, died: 1989 },
21+
// })
22+
// should return {
23+
// lenin: 54,
24+
// mao: 83,
25+
// gandhi: 79,
26+
// hirohito: 88,
27+
// }

0 commit comments

Comments
 (0)