Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iteration TASKS 1 - 7 #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Exercises/1-for.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'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;

for (let i = 0; i < args.length; ++i) {
answer += args[i];
}

return answer;
};

module.exports = { sum };
10 changes: 7 additions & 3 deletions Exercises/2-for-of.js
Original file line number Diff line number Diff line change
@@ -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 };
10 changes: 7 additions & 3 deletions Exercises/3-while.js
Original file line number Diff line number Diff line change
@@ -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 answer = 0;

while (args.length > 0) {
answer += args.pop();
}

return answer;
};

module.exports = { sum };
12 changes: 9 additions & 3 deletions Exercises/4-do-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'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;

if (args.length === 0) return answer;

do {
answer += args.pop();
} while (args.length > 0);

return answer;
};

module.exports = { sum };
5 changes: 1 addition & 4 deletions Exercises/5-reduce.js
Original file line number Diff line number Diff line change
@@ -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 };
13 changes: 10 additions & 3 deletions Exercises/6-matrix.js
Original file line number Diff line number Diff line change
@@ -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 };
21 changes: 8 additions & 13 deletions Exercises/7-ages.js
Original file line number Diff line number Diff line change
@@ -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 };