From 773572b91ade1caf65c3552de1e01dde958334cd Mon Sep 17 00:00:00 2001 From: Maks3105 Date: Fri, 3 May 2024 20:48:26 +0700 Subject: [PATCH 1/2] while --- Exercises/1-for.js | 11 ++++++++--- Exercises/2-for-of.js | 10 +++++++--- Exercises/3-while.js | 12 +++++++++--- Exercises/4-do-while.js | 15 ++++++++++++--- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..c302f8a 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 answer = 0; + const len = args.length; + + for (let i = 0; i < len; ++i) { + answer += args[i]; + } + + return answer; }; module.exports = { sum }; diff --git a/Exercises/2-for-of.js b/Exercises/2-for-of.js index 9965f25..b324211 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 answer = 0; + + for (const num of args) { + answer += num; + } + + return answer; }; module.exports = { sum }; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 6110b9f..61dca3c 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -1,9 +1,15 @@ '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 answer = 0; + let num = args.pop(); + + while (num) { + answer += num; + num = args.pop(); + } + + return answer; }; module.exports = { sum }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 22d4464..5475b1f 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -1,9 +1,18 @@ '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 answer = 0; + + do { + let num = args.pop(); + } while (args.length > 0) { + answer += num; + num = args.pop(); + } + + return answer; }; +console.log(sum()); + module.exports = { sum }; From fb3aa94defc939d3c42406e1180991b48c75851a Mon Sep 17 00:00:00 2001 From: Maks3105 Date: Sat, 4 May 2024 12:18:49 +0700 Subject: [PATCH 2/2] Iteration TASKS 1 - 7 --- Exercises/1-for.js | 3 +-- Exercises/3-while.js | 6 ++---- Exercises/4-do-while.js | 11 ++++------- Exercises/5-reduce.js | 5 +---- Exercises/6-matrix.js | 13 ++++++++++--- Exercises/7-ages.js | 21 ++++++++------------- 6 files changed, 26 insertions(+), 33 deletions(-) diff --git a/Exercises/1-for.js b/Exercises/1-for.js index c302f8a..1e10361 100644 --- a/Exercises/1-for.js +++ b/Exercises/1-for.js @@ -2,9 +2,8 @@ const sum = (...args) => { let answer = 0; - const len = args.length; - for (let i = 0; i < len; ++i) { + for (let i = 0; i < args.length; ++i) { answer += args[i]; } diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 61dca3c..48f1b2f 100644 --- a/Exercises/3-while.js +++ b/Exercises/3-while.js @@ -2,11 +2,9 @@ const sum = (...args) => { let answer = 0; - let num = args.pop(); - while (num) { - answer += num; - num = args.pop(); + while (args.length > 0) { + answer += args.pop(); } return answer; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 5475b1f..6fc2674 100644 --- a/Exercises/4-do-while.js +++ b/Exercises/4-do-while.js @@ -3,16 +3,13 @@ const sum = (...args) => { let answer = 0; + if (args.length === 0) return answer; + do { - let num = args.pop(); - } while (args.length > 0) { - answer += num; - num = args.pop(); - } + answer += args.pop(); + } while (args.length > 0); return answer; }; -console.log(sum()); - module.exports = { sum }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..9840bca 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((a, b) => a + b, 0); module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index 0bc42c8..80c4ec5 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 + let maxNum = 0; + + for (let i = 0; i < matrix.length; ++i) { + for (let j = 0; j < matrix[i].length; ++j) { + const num = matrix[i][j]; + maxNum = num > maxNum ? num : maxNum; + } + } + + return maxNum; }; module.exports = { max }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index e90caa2..e7f4e0b 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 arrYears = {}; + + for (const people in persons) { + const age = persons[people].died - persons[people].born; + arrYears[people] = age; + } + + return arrYears; }; module.exports = { ages };