From d67a8d02932edf23ee6becaae22bba860d939fa2 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sat, 4 Jan 2020 14:39:30 +0200 Subject: [PATCH 1/3] Good solution for matrix(6-matrix.js) --- Exercises/1-for.js | 9 ++++++--- Exercises/2-for-of.js | 8 +++++--- Exercises/3-while.js | 9 ++++++--- Exercises/4-do-while.js | 11 ++++++++--- Exercises/5-reduce.js | 6 ++---- Exercises/6-matrix.js | 12 +++++++++--- Exercises/7-ages.js | 19 ++++++------------- 7 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..3e6553a 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -1,9 +1,12 @@ '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; + if (args) { + for (let i = 0; i < args.length; i++) { + res += args[i]; + }} + return res; }; module.exports = { sum }; diff --git a/Exercises/2-for-of.js b/Exercises/2-for-of.js index 9965f25..888c143 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 res = 0; + if (args) { + for (const value of args) res += value; + } + return res; }; module.exports = { sum }; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 6110b9f..dd0445e 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -1,9 +1,12 @@ '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 res = 0; + if (args) { + let i = 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..3b6a6e9 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 res = 0; + if (args[0]) { let i = 0; + do { + res += args[i]; + i = i + 1; + } while (i < args.length); + }; + return res; }; module.exports = { sum }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..78284f7 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, x) => acc + x, 0); + module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index b768c0a..f6f0267 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 + let list = []; + for (let elem of matrix) { + for (let inside of elem) { + list.push(inside); + } + } + // easy gggggggggggggggggggggggggggggggg + return list.reduce((acc, x) => acc > x ? acc : x); }; + module.exports = { max }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index fc8089b..f420dfe 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 res = {}; + for (const key in persons) { + // value = date of death - date of birth + res[key] = persons[key].died - persons[key].born; + } + return res; }; module.exports = { ages }; From 52e61dbfa62d4aa1dcaa6001cdd9be971b617f03 Mon Sep 17 00:00:00 2001 From: Vladlinu <33317989+vladlinu@users.noreply.github.com> Date: Sat, 4 Jan 2020 14:42:41 +0200 Subject: [PATCH 2/3] Update 6-matrix.js --- Exercises/6-matrix.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index f6f0267..cec524b 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -7,7 +7,7 @@ const max = matrix => { list.push(inside); } } - // easy gggggggggggggggggggggggggggggggg + // gggggggggggggggggggggggggggggggg return list.reduce((acc, x) => acc > x ? acc : x); }; From 4f39ee37c6182230ade7ecf82c7449f385080dc4 Mon Sep 17 00:00:00 2001 From: Vladlinu <33317989+vladlinu@users.noreply.github.com> Date: Sat, 4 Jan 2020 14:44:42 +0200 Subject: [PATCH 3/3] Update 6-matrix.js --- Exercises/6-matrix.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index cec524b..5e0482c 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -7,7 +7,7 @@ const max = matrix => { list.push(inside); } } - // gggggggggggggggggggggggggggggggg + // find max value in list of all elements with reduce return list.reduce((acc, x) => acc > x ? acc : x); };