Skip to content

Iteration lab done #45

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Iteration.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 result = 0;

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

return result;
};

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 result = 0;

for (const item of args) {
result += item;
}

return result;
};

module.exports = { sum };
12 changes: 9 additions & 3 deletions Exercises/3-while.js
Original file line number Diff line number Diff line change
@@ -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 i = 0;
let result = 0;

while (i < args.length) {
result += args[i];
i++;
}

return result;
};

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 i = 0;
let r = 0;

do {
r += args[i];
i++;
} while (i < args.length);

return args.length ? r : 0;
};

module.exports = { sum };
6 changes: 2 additions & 4 deletions Exercises/5-reduce.js
Original file line number Diff line number Diff line change
@@ -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, value) => acc += value, 0);


module.exports = { sum };
16 changes: 12 additions & 4 deletions Exercises/6-matrix.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
'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
const max = (matrix) => {
let maxValue = 0;

for (let i = 0; i < matrix.length; i++) {
for (let k = 0; k < matrix[i].length; k++) {
const nestedArrayValue = matrix[i][k];

if (maxValue < nestedArrayValue) maxValue = nestedArrayValue;
}
}

return maxValue;
};

module.exports = { max };
23 changes: 9 additions & 14 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 ages = (persons) => {
const ages = {};

for (const person in persons) {
const current = persons[person];
ages[person] = current.died - current.born;
}

return ages;
};

module.exports = { ages };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",
"license": "MIT",
"scripts": {
"test": "eslint ./Exercises; hpw"
"test": "eslint ./Exercises && hpw"
},
"dependencies": {
"eslint": "^7.23.0",
Expand Down