diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..420d433 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -1,9 +1,15 @@ 'use strict'; +// Use for loop and accumulator variable +// to calculate sum of all given arguments +// For example sum(1, 2, 3) should return 6 + 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 result = 0; + for (let i = 0; i < args.length; i++) { + result += args[i]; + } + return result; }; module.exports = { sum }; diff --git a/Exercises/2-for-of.js b/Exercises/2-for-of.js index 9965f25..d8dd95b 100644 --- a/Exercises/2-for-of.js +++ b/Exercises/2-for-of.js @@ -1,9 +1,15 @@ 'use strict'; +// Use for..of loop and accumulator variable +// to calculate sum of all given arguments +// For example sum(1, 2, 3) should return 6 + 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 result = 0; + for (const item of args) { + result += item; + } + return result; }; module.exports = { sum }; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 6110b9f..21ed3e1 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -1,9 +1,17 @@ 'use strict'; +// Use while loop and accumulator variable +// to calculate sum of all given arguments +// For example sum(1, 2, 3) should return 6 + 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 }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 22d4464..4e0e87f 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -1,9 +1,18 @@ 'use strict'; +// Use do..while loop and accumulator variable +// to calculate sum of all given arguments +// For example sum(1, 2, 3) should return 6 + 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 }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..820bd75 100644 --- a/Exercises/5-reduce.js +++ b/Exercises/5-reduce.js @@ -1,8 +1,9 @@ '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, value) => acc + value, 0); + module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index 0bc42c8..63d56d5 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -1,9 +1,22 @@ 'use strict'; +// 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 + +const MAX = (a, b) => (a > b ? a : b); + 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 + // Lorem ipsum dolor sit amet, consectetur adipiscing elit. + let maxValue = matrix[0][0]; + + for (const row of matrix) { + for (const item of row) { + maxValue = MAX(item, maxValue); + } + } + + return maxValue; }; module.exports = { max }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index e90caa2..9bccee6 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -1,19 +1,30 @@ 'use strict'; + +// 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 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 result = {}; + + for (const name in persons) { + const person = persons[name]; + result[name] = person.died - person.born; + } + + return result; }; module.exports = { ages };