From 75fea092531556f9c8996632e61f7f53bc631945 Mon Sep 17 00:00:00 2001 From: Katywolk Date: Tue, 4 Mar 2025 15:10:29 +0800 Subject: [PATCH] HomeWork Iteration --- Exercises/1-for.js | 9 ++++++--- Exercises/2-for-of.js | 8 +++++--- Exercises/3-while.js | 10 +++++++--- Exercises/4-do-while.js | 11 ++++++++--- Exercises/5-reduce.js | 5 +---- Exercises/6-matrix.js | 18 +++++++++++++++--- Exercises/7-ages.js | 18 +++++------------- 7 files changed, 47 insertions(+), 32 deletions(-) diff --git a/Exercises/1-for.js b/Exercises/1-for.js index 62e6ab8..25314c8 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 total = 0; + + for (let i = 0; i < args.length; i++) { + total += args[i]; + } + return total; }; module.exports = { sum }; diff --git a/Exercises/2-for-of.js b/Exercises/2-for-of.js index 9965f25..4495f77 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 total = 0; + for (const number of args) { + total += number; + } + return total; }; module.exports = { sum }; diff --git a/Exercises/3-while.js b/Exercises/3-while.js index 6110b9f..854ea8a 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 total = 0; + let i = 0; + while (i < args.length) { + total += args[i]; + i++; + } + return total; }; module.exports = { sum }; diff --git a/Exercises/4-do-while.js b/Exercises/4-do-while.js index 22d4464..14f8a7a 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 total = 0; + let i = 0; + + do { + total += args[i] || 0; + i++; + } while (i < args.length); + return total; }; module.exports = { sum }; diff --git a/Exercises/5-reduce.js b/Exercises/5-reduce.js index a9cb44c..e6dfd6d 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, num) => acc + num, 0); module.exports = { sum }; diff --git a/Exercises/6-matrix.js b/Exercises/6-matrix.js index 0bc42c8..d49b9f2 100644 --- a/Exercises/6-matrix.js +++ b/Exercises/6-matrix.js @@ -1,9 +1,21 @@ '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 maxValue = -Infinity; + + const findMax = (arr) => { + for (const el of arr) { + if (Array.isArray(el)) { + findMax(el); + } else { + maxValue = Math.max(maxValue, el); + } + } + }; + + findMax(matrix); + return maxValue; }; + module.exports = { max }; diff --git a/Exercises/7-ages.js b/Exercises/7-ages.js index e90caa2..a35892f 100644 --- a/Exercises/7-ages.js +++ b/Exercises/7-ages.js @@ -1,19 +1,11 @@ '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) { + result[person] = persons[person].died - persons[person].born; + } + return result; }; module.exports = { ages };