-
-
Notifications
You must be signed in to change notification settings - Fork 120
Exercise is done #25
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
Exercise is done #25
Changes from all commits
096e8ee
9442132
9c7edda
d20b5b6
bebe19a
3d19ce5
2c13705
a27e36d
e610114
98c6094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Упражнения | ||
|
||
## Итерирование циклами | ||
|
||
Реализуйте функцию `sum(...args)`, которая суммирует все свои аргументы, пятью | ||
разными способами. Примеры вызовов с результатами: | ||
```js | ||
const a = sum(1, 2, 3) // a === 6 | ||
const b = sum(0) // b === 0 | ||
const c = sum() // c === 0 | ||
const d = sum(1, -1, 1) // d === 1 | ||
const e = sum(10, -1, -1, -1) // e === 7 | ||
``` | ||
|
||
1. Цикл `for` | ||
2. Цикл `for..of` | ||
3. Цикл `while` | ||
4. Цикл `do..while` | ||
5. Метод `Array.prototype.reduce()` | ||
|
||
## Итерирование по двумерному массиву | ||
|
||
6. Найдите максимальный элемент в двумерном массиве | ||
```js | ||
const m = max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
console.log(m); // 9 | ||
``` | ||
|
||
## Итерирование объектов-справочников | ||
|
||
7. При помощи цикла `for..in` перебрать объект-справочник с датами рождения и | ||
смерти людей и вернуть справочник с продолжительностью их жизни. Например: | ||
```js | ||
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)); | ||
// { | ||
// lenin: 54, | ||
// mao: 83, | ||
// gandhi: 79, | ||
// hirohito: 88, | ||
// } | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
'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; args[i]; i++) { | ||
sum += args[i]; | ||
} | ||
return sum; | ||
}; | ||
|
||
module.exports = { sum }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
'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 i of args) { | ||
sum += i; | ||
} | ||
return sum; | ||
}; | ||
|
||
module.exports = { sum }; |
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 i = 0; | ||
let sum = 0; | ||
while (args[i]) { | ||
sum += args[i]; | ||
i++; | ||
} | ||
return sum; | ||
}; | ||
|
||
module.exports = { sum }; |
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 i = 0, | ||||||
sum = 0; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
do { | ||||||
sum += args[i]; | ||||||
i++; | ||||||
} while (args[i]); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return sum; | ||||||
}; | ||||||
|
||||||
module.exports = { sum }; |
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, sum) => (acc += sum), 0); | ||
|
||
|
||
module.exports = { sum }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const max = matrix => { | ||
//something to make solution longer | ||
//something to make solution longer | ||
let max = matrix[0][0]; | ||
for (const i of matrix) { | ||
for (const j of i) { | ||
if (max < j) max = j; | ||
} | ||
} | ||
return max; | ||
}; | ||
module.exports = { max }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
({ | ||
name: 'max', | ||
length: [220, 300], | ||
cases: [ | ||
[[[10]], 10], | ||
[[[1, 2], [3, 4], [5, 6]], 6], | ||
[[[-1, 1], [2, -1], [-1, 0]], 2], | ||
], | ||
test: max => { | ||
const src = max.toString(); | ||
if (!src.includes('for (')) throw new Error('Use for loop'); | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,10 @@ | ||||||||
'use strict'; | ||||||||
|
||||||||
const ages = persons => { | ||||||||
const ageList = {}; | ||||||||
for (const index in persons) { | ||||||||
ageList[index] = persons[index]['died'] - persons[index]['born']; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
return ageList; | ||||||||
}; | ||||||||
module.exports = { ages }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
({ | ||
name: 'ages', | ||
length: [150, 190], | ||
cases: [ | ||
[ | ||
{ | ||
lenin: { born: 1870, died: 1924 }, | ||
mao: { born: 1893, died: 1976 }, | ||
gandhi: { born: 1869, died: 1948 }, | ||
hirohito: { born: 1901, died: 1989 }, | ||
}, { | ||
lenin: 54, | ||
mao: 83, | ||
gandhi: 79, | ||
hirohito: 88, | ||
} | ||
] | ||
], | ||
test: ages => { | ||
const src = ages.toString(); | ||
if (!src.includes('for (')) throw new Error('Use for..in loop'); | ||
if (!src.includes(' in ')) throw new Error('Use for..in loop'); | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Different implementation of iterations as a code abstraction | ||
|
||
[](https://www.youtube.com/watch?v=/VBMGnAPfmsY) | ||
[](https://www.youtube.com/watch?v=VBMGnAPfmsY) | ||
[](https://www.youtube.com/watch?v=lq3b5_UGJas) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const max = matrix => { | ||
let value = matrix[0][0]; | ||
for (let i = 0; i < matrix.length; i++) { | ||
const row = matrix[i]; | ||
for (let j = 0; j < row.length; j++) { | ||
const cell = row[j]; | ||
if (value < cell) value = cell; | ||
} | ||
} | ||
return value; | ||
}; | ||
|
||
module.exports = { max }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const ages = persons => { | ||
const data = {}; | ||
for (const name in persons) { | ||
const person = persons[name]; | ||
data[name] = person.died - person.born; | ||
} | ||
return data; | ||
}; | ||
|
||
module.exports = { ages }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "eslint ./Exercises; hpw" | ||
"test": "eslint ./Exercises && hpw" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't change |
||
}, | ||
"dependencies": { | ||
"eslint": "^6.4.0", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.