diff --git a/.eslintrc.json b/.eslintrc.json index 188baf7..9b0f412 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,7 @@ ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", @@ -265,4 +265,4 @@ "never" ] } -} +} \ No newline at end of file diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..10b9a08 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -1,9 +1,14 @@ '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 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..e540ee7 100644 --- a/Exercises/2-for-of.js +++ b/Exercises/2-for-of.js @@ -1,9 +1,13 @@ '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 result = 0; + + for (const val of args) { + result += val; + } + + return result; }; module.exports = { sum }; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 6110b9f..9297038 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -1,9 +1,14 @@ '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 i = 0; + let res = 0; + + while (i < args.length) { + res += args[i++]; + } + + return res; }; module.exports = { sum }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 22d4464..16c331a 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -1,9 +1,16 @@ '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) return 0; + let i = 0; + let res = 0; + + do { + res += args[i++]; + } while (i < args.length); + + return res; }; + module.exports = { sum }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..d173537 100644 --- a/Exercises/5-reduce.js +++ b/Exercises/5-reduce.js @@ -1,8 +1,6 @@ '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, val) => acc += val, 0); + module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index b768c0a..f94b2f7 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -1,9 +1,16 @@ '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 + const arrOfNum = []; + + for (let i = 0; i < matrix.length; i++) { + const row = matrix[i]; + for (let j = 0; j < row.length; j++) { + arrOfNum.push(Math.max.apply(null, row)); + } + } + + return Math.max.apply(null, arrOfNum); }; module.exports = { max }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index fc8089b..63d185c 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -1,19 +1,14 @@ '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 personAge = {}; + + for (const person in persons) { + const { died, born } = persons[person]; + personAge[person] = died - born; + } + + return personAge; }; module.exports = { ages }; diff --git a/package.json b/package.json index c545366..4ef755a 100644 --- a/package.json +++ b/package.json @@ -11,4 +11,4 @@ "eslint": "^7.17.0", "hpw": "^0.1.14" } -} +} \ No newline at end of file