-
-
Notifications
You must be signed in to change notification settings - Fork 121
All labs done #24
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
Open
lizardlynx
wants to merge
10
commits into
HowProgrammingWorks:master
Choose a base branch
from
lizardlynx:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
All labs done #24
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
096e8ee
Fix video
tshemsedinov 9442132
Merge branch 'master' of github.com:HowProgrammingWorks/Iteration
tshemsedinov 9c7edda
Add nested loops task
tshemsedinov d20b5b6
Add for..in task
tshemsedinov bebe19a
Add exercises description
tshemsedinov 3d19ce5
Use pop instead of shift
tshemsedinov 2c13705
Update Exercises.ru.md (#19)
ArMANIAK 67bb991
All Labs Done
lizardlynx 42ae4c8
Update 7-ages.js
lizardlynx 4e12cd0
Update Exercises/5-reduce.js
lizardlynx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
// } | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
'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; | ||
}; | ||
|
||
module.exports = { sum }; | ||
|
||
// Use for loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
'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 arg of args) { | ||
sum += arg; | ||
} | ||
return sum; | ||
}; | ||
|
||
module.exports = { sum }; | ||
|
||
// Use for..of loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
'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 (i < args.length) { | ||
sum += args[i]; | ||
i++; | ||
} | ||
return sum; | ||
}; | ||
|
||
module.exports = { sum }; | ||
|
||
// Use while loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
'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 sum = 0; | ||
if (args.length == 0) return 0; | ||
do { | ||
sum += args[i]; | ||
i++; | ||
} while (i < args.length); | ||
return sum; | ||
}; | ||
|
||
module.exports = { sum }; | ||
|
||
// Use do..while loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
'use strict'; | ||
|
||
const sum = (...args) => 0; | ||
const sum = (...args) => args.reduce((s, c) => s += c, 0); | ||
|
||
module.exports = { sum }; | ||
|
||
// Use Array.prototype.reduce method | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 | ||
|
||
module.exports = { sum }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const max = matrix => { | ||
let max = 0; | ||
for (const array of matrix) { | ||
for (const item of array) { | ||
if (item > max) { | ||
max = item; | ||
} | ||
} | ||
} | ||
return max; | ||
}; | ||
|
||
module.exports = { max }; | ||
|
||
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const ages = persons => { | ||
const ages = {}; | ||
for (const name in persons) { | ||
const person = persons[name]; | ||
ages[name] = person.died - person.born; | ||
} | ||
return ages; | ||
}; | ||
|
||
module.exports = { ages }; | ||
|
||
// 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, | ||
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.