Skip to content
Prev Previous commit
Next Next commit
Labs 1-7
  • Loading branch information
MaksGovor committed Oct 19, 2019
commit 1ca9ca9411d143d490e93f012da7ed01a8119523
7 changes: 4 additions & 3 deletions Exercises/1-for.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

const sum = (...args) => {
// Use for loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
let res = 0;
for (let i = 0; i < args.length; i++) {
res += args[i];
} return res;
};

module.exports = { sum };
7 changes: 4 additions & 3 deletions Exercises/2-for-of.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

const sum = (...args) => {
// Use for..of loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
let res = 0;
for (const value of args) {
res += value;
} return res;
};

module.exports = { sum };
7 changes: 4 additions & 3 deletions Exercises/3-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

const sum = (...args) => {
// Use while loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
let result = 0;
while (args.length > 0) {
result += args.pop();
} return result;
};

module.exports = { sum };
9 changes: 6 additions & 3 deletions Exercises/4-do-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

const sum = (...args) => {
// Use do..while loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
if (args.length === 0) return 0;
let result = 0;
do {
result += args.pop();
} while (args.length > 0);
return result;
};

module.exports = { sum };
5 changes: 1 addition & 4 deletions Exercises/5-reduce.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const sum = (...args) => 0;
// Use Array.prototype.reduce method
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
const sum = (...args) => args.reduce(((acc, cur) => acc + cur), 0);

module.exports = { sum };
29 changes: 26 additions & 3 deletions Exercises/6-matrix.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
'use strict';

const max = matrix => {
// Use nested for loop to find max value in 2d matrix
// For example max([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
// should return 9
let value = matrix[0][0];
for (const index in matrix) {
const line = matrix[index];
for (const index2 in line) {
const colum = line[index2];
if (colum > value) value = colum;
}
}
return value;
};

/*const max = matrix => {
let value = matrix[0][0];
for (const arr of matrix) {
for (const i of arr) {
if (i > value) value = i;
}
}
return value;
};*/

/*const max = matrix => {
const modeMatrix = matrix
.map(arr => arr.reduce((a, b) => (a > b ? a : b)))
.reduce((a, b) => (a > b ? a : b));
return modeMatrix;
};*/

module.exports = { max };
19 changes: 6 additions & 13 deletions Exercises/7-ages.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
'use strict';

const ages = persons => {
// Use for..in to calculate age for each person
// For example ages({
// lenin: { born: 1870, died: 1924 },
// mao: { born: 1893, died: 1976 },
// gandhi: { born: 1869, died: 1948 },
// hirohito: { born: 1901, died: 1989 },
// })
// should return {
// lenin: 54,
// mao: 83,
// gandhi: 79,
// hirohito: 88,
// }
const age = {};
for (const key in persons) {
const person = persons[key];
age[key] = person.died - person.born;
}
return age;
};

module.exports = { ages };