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 #36

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
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"linebreak-style": [
"error",
"unix"
"windows"
],
"quotes": [
"error",
Expand Down Expand Up @@ -265,4 +265,4 @@
"never"
]
}
}
}
11 changes: 8 additions & 3 deletions Exercises/1-for.js
Original file line number Diff line number Diff line change
@@ -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 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 val of args) {
result += val;
}

return result;
};

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

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

return res;
};

module.exports = { sum };
13 changes: 10 additions & 3 deletions Exercises/4-do-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'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
if (!args.length) return 0;
let i = 0;
let res = 0;

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

return res;
};


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, val) => acc += val, 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
const arrOfNum = [];

for (let i = 0; i < matrix.length; i++) {
const row = matrix[i];
for (let j = 0; j < row.length; j++) {
arrOfNum.push(Math.max.apply(null, row));
}
}

return Math.max.apply(null, arrOfNum);
};

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 personAge = {};

for (const person in persons) {
const { died, born } = persons[person];
personAge[person] = died - born;
}

return personAge;
};

module.exports = { ages };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"eslint": "^7.17.0",
"hpw": "^0.1.14"
}
}
}