From c64aed1691da98ec15f6db407975f0b43653f004 Mon Sep 17 00:00:00 2001 From: Orbis Date: Sun, 30 Jan 2022 20:13:08 +0300 Subject: [PATCH 1/4] homework --- Exercises/1-for.js | 5 +++++ Exercises/2-for-of.js | 5 +++++ Exercises/3-while.js | 7 +++++++ Exercises/4-do-while.js | 7 +++++++ Exercises/5-reduce.js | 2 +- Exercises/6-matrix.js | 11 ++++++++++- Exercises/7-ages.js | 8 +++++++- JavaScript/5-for-in-array.js | 2 ++ 8 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..e78df69 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -4,6 +4,11 @@ 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..3ddb644 100644 --- a/Exercises/2-for-of.js +++ b/Exercises/2-for-of.js @@ -4,6 +4,11 @@ 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..b0a15fb 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -4,6 +4,13 @@ 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 step = 0; + while (step < args.length) { + acc += args[step]; + step++; + } + return acc; }; module.exports = { sum }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 22d4464..f5b5834 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -4,6 +4,13 @@ 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 step = 0; + do { + acc += args[step]; + step++; + } while (step <= args.length); + return acc; }; module.exports = { sum }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..491f324 100644 --- a/Exercises/5-reduce.js +++ b/Exercises/5-reduce.js @@ -1,6 +1,6 @@ 'use strict'; -const sum = (...args) => 0; +const sum = (...args) => args.reduce((acc, value) => acc + value); // Use Array.prototype.reduce method // to calculate sum of all given arguments // For example sum(1, 2, 3) should return 6 diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index b768c0a..ab60e3f 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -1,9 +1,18 @@ 'use strict'; -const max = matrix => { +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 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..694eebe 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -1,6 +1,6 @@ 'use strict'; -const ages = persons => { +const ages = (persons) => { // Use for..in to calculate age for each person // For example ages({ // lenin: { born: 1870, died: 1924 }, @@ -14,6 +14,12 @@ const ages = persons => { // gandhi: 79, // hirohito: 88, // } + const result = {}; + for (const person in persons) { + const { born, died } = 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) From f36db676cb69da218940d0e905b058f19b7544ec Mon Sep 17 00:00:00 2001 From: Orbis Date: Sun, 30 Jan 2022 20:17:34 +0300 Subject: [PATCH 2/4] fix reduce for empty array --- Exercises/5-reduce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index 491f324..a099383 100644 --- a/Exercises/5-reduce.js +++ b/Exercises/5-reduce.js @@ -1,6 +1,6 @@ 'use strict'; -const sum = (...args) => args.reduce((acc, value) => acc + value); +const sum = (...args) => args.reduce((acc, value) => acc + value, 0); // Use Array.prototype.reduce method // to calculate sum of all given arguments // For example sum(1, 2, 3) should return 6 From 31ea02623e19e49c6aba3ba06dc0a72a57c2ff29 Mon Sep 17 00:00:00 2001 From: Orbis Date: Sun, 30 Jan 2022 20:19:34 +0300 Subject: [PATCH 3/4] delete all comments --- Exercises/1-for.js | 3 --- Exercises/2-for-of.js | 3 --- Exercises/3-while.js | 3 --- Exercises/4-do-while.js | 3 --- Exercises/5-reduce.js | 3 --- Exercises/6-matrix.js | 3 --- Exercises/7-ages.js | 13 ------------- 7 files changed, 31 deletions(-) diff --git a/Exercises/1-for.js b/Exercises/1-for.js index e78df69..186ef21 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -1,9 +1,6 @@ '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]; diff --git a/Exercises/2-for-of.js b/Exercises/2-for-of.js index 3ddb644..37278f9 100644 --- a/Exercises/2-for-of.js +++ b/Exercises/2-for-of.js @@ -1,9 +1,6 @@ '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; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index b0a15fb..9db8f24 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -1,9 +1,6 @@ '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 step = 0; while (step < args.length) { diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index f5b5834..5fb59a8 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -1,9 +1,6 @@ '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 step = 0; do { diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a099383..134e175 100644 --- a/Exercises/5-reduce.js +++ b/Exercises/5-reduce.js @@ -1,8 +1,5 @@ 'use strict'; const sum = (...args) => args.reduce((acc, value) => acc + value, 0); -// Use Array.prototype.reduce method -// to calculate sum of all given arguments -// For example sum(1, 2, 3) should return 6 module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index ab60e3f..a8e03e9 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -1,9 +1,6 @@ '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 max = 0; for (let row = 0; row < matrix.length; row++) { for (let col = 0; col < matrix[row].length; col++) { diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index 694eebe..dcf1fc8 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -1,19 +1,6 @@ '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 result = {}; for (const person in persons) { const { born, died } = person; From 10f4480741574ed88fb8aa8b9fd628e2f9b3051b Mon Sep 17 00:00:00 2001 From: Orbis Date: Sun, 30 Jan 2022 20:49:06 +0300 Subject: [PATCH 4/4] fix solution lemgs and errors --- Exercises/3-while.js | 8 ++++---- Exercises/4-do-while.js | 9 +++++---- Exercises/7-ages.js | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 9db8f24..c260099 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -2,10 +2,10 @@ const sum = (...args) => { let acc = 0; - let step = 0; - while (step < args.length) { - acc += args[step]; - step++; + let i = 0; + while (i < args.length) { + acc += args[i]; + i++; } return acc; }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 5fb59a8..8c83a86 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -2,11 +2,12 @@ const sum = (...args) => { let acc = 0; - let step = 0; + let i = 0; + if (!args[i]) return 0; do { - acc += args[step]; - step++; - } while (step <= args.length); + acc += args[i]; + i++; + } while (i < args.length); return acc; }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index dcf1fc8..5639627 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -3,7 +3,7 @@ const ages = (persons) => { const result = {}; for (const person in persons) { - const { born, died } = person; + const { born, died } = persons[person]; result[person] = died - born; } return result;