Skip to content
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
2 changes: 1 addition & 1 deletion .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
10 changes: 6 additions & 4 deletions Exercises/1-for.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'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 sum = 0;
for (let i = 0; i < args.length; i++) {
sum += args[i];
}
return sum;
};

console.log(sum(1, 2, 3));
module.exports = { sum };
10 changes: 6 additions & 4 deletions Exercises/2-for-of.js
Original file line number Diff line number Diff line change
@@ -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 sum = 0;
for (const element of args) {
sum += element;
}
return sum;
};

console.log(sum());
module.exports = { sum };
11 changes: 7 additions & 4 deletions Exercises/3-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'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 sum = 0;
let i = 0;
while (i < args.length) {
sum += args[i++];
}
return sum;
};

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

console.log(sum(1, 2, 3, 6));
module.exports = { sum };
7 changes: 2 additions & 5 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((prev, cur) => prev + cur, 0);
console.log(sum(0, 3, -5));
module.exports = { sum };
17 changes: 12 additions & 5 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 max = (matrix) => {
let max = matrix[0][0];
for (let i = 0; i < matrix.length; i++) {
const row = matrix[i];
for (let j = 0; j < row.length; j++) {
if (max < row[j])
max = row[j];
}
}
return max;
};

const m = max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
console.log(m);
module.exports = { max };
28 changes: 13 additions & 15 deletions Exercises/7-ages.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'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 personsKey in persons) {
ages[personsKey] = persons[personsKey].died - persons[personsKey].born;
}
return ages;
};

const persons = {
lenin: { born: 1870, died: 1924 },
mao: { born: 1893, died: 1976 },
gandhi: { born: 1869, died: 1948 },
hirohito: { born: 1901, died: 1989 },
};
console.log(ages(persons));
module.exports = { ages };
Loading