diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..186ef21 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -1,9 +1,11 @@ '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 acc = 0; + for (let i = 0; i < args.length; i++) { + acc += args[i]; + } + return acc; }; module.exports = { sum }; diff --git a/Exercises/2-for-of.js b/Exercises/2-for-of.js index 9965f25..37278f9 100644 --- a/Exercises/2-for-of.js +++ b/Exercises/2-for-of.js @@ -1,9 +1,11 @@ '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 acc = 0; + for (const arg of args) { + acc += arg; + } + return acc; }; module.exports = { sum }; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 6110b9f..c260099 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -1,9 +1,13 @@ '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 acc = 0; + let i = 0; + while (i < args.length) { + acc += args[i]; + i++; + } + return acc; }; module.exports = { sum }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 22d4464..8c83a86 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -1,9 +1,14 @@ '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 + let acc = 0; + let i = 0; + if (!args[i]) return 0; + do { + acc += args[i]; + i++; + } while (i < args.length); + return acc; }; module.exports = { sum }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..134e175 100644 --- a/Exercises/5-reduce.js +++ b/Exercises/5-reduce.js @@ -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, value) => acc + value, 0); module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index b768c0a..a8e03e9 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -1,9 +1,15 @@ '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 max = (matrix) => { + let max = 0; + for (let row = 0; row < matrix.length; row++) { + for (let col = 0; col < matrix[row].length; col++) { + const val = matrix[row][col]; + max = val > max ? val : max; + } + } + return max; + }; module.exports = { max }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index fc8089b..5639627 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -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 ages = (persons) => { + const result = {}; + for (const person in persons) { + const { born, died } = persons[person]; + result[person] = died - born; + } + return result; }; module.exports = { ages }; diff --git a/JavaScript/5-for-in-array.js b/JavaScript/5-for-in-array.js index c78e165..6316335 100644 --- a/JavaScript/5-for-in-array.js +++ b/JavaScript/5-for-in-array.js @@ -17,3 +17,5 @@ for (const i in numbers) { const value = numbers[i]; console.log(i, typeof i, value); } + +console.log(numbers.newField)