├── .eslintignore ├── .github └── FUNDING.yml ├── .gitignore ├── knowledge.map ├── JavaScript ├── 2-while.js ├── 3-do-while.js ├── 8-continue.js ├── 7-break.js ├── c-matrix-each.js ├── d-matrix-for-in.js ├── g-reverse.js ├── 9-forEach.js ├── 1-for.js ├── e-reduce.js ├── b-matrix.js ├── 5-for-in-array.js ├── 4-for-in-obj.js ├── a-map.js ├── h-async.js ├── f-iterator.js ├── 6-for-of.js └── i-async.js ├── Solutions ├── 5-reduce.js ├── 2-for-of.js ├── 3-while.js ├── 1-for.js ├── 4-do-while.js ├── 7-ages.js └── 6-matrix.js ├── Exercises ├── 5-reduce.js ├── 1-for.js ├── 6-matrix.js ├── 2-for-of.js ├── 3-while.js ├── 4-do-while.js ├── 6-matrix.test ├── 5-reduce.test ├── 1-for.test ├── 3-while.test ├── 7-ages.js ├── 4-do-while.test ├── 2-for-of.test └── 7-ages.test ├── .editorconfig ├── README.md ├── package.json ├── LICENSE ├── Exercises.ru.md └── .eslintrc.json /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: tshemsedinov 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /knowledge.map: -------------------------------------------------------------------------------- 1 | { 2 | dependencies: ['Function'] 3 | } 4 | -------------------------------------------------------------------------------- /JavaScript/2-while.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | let a = 0; 4 | while (a < 10) { 5 | console.log(a++); 6 | } 7 | -------------------------------------------------------------------------------- /JavaScript/3-do-while.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | let i = 0; 4 | do { 5 | console.log(i++); 6 | } while (i < 10); 7 | -------------------------------------------------------------------------------- /Solutions/5-reduce.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => args.reduce((a, b) => (a + b), 0); 4 | 5 | module.exports = { sum }; 6 | -------------------------------------------------------------------------------- /JavaScript/8-continue.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | let i = 0; 4 | while (i < 10) { 5 | i++; 6 | console.log('Hello', i); 7 | if (i === 5) continue; 8 | console.log('World'); 9 | } 10 | -------------------------------------------------------------------------------- /Solutions/2-for-of.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | let value = 0; 5 | for (const arg of args) value += arg; 6 | return value; 7 | }; 8 | 9 | module.exports = { sum }; 10 | -------------------------------------------------------------------------------- /Solutions/3-while.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | let value = 0; 5 | while (args.length > 0) { 6 | value += args.pop(); 7 | } 8 | return value; 9 | }; 10 | 11 | module.exports = { sum }; 12 | -------------------------------------------------------------------------------- /Exercises/5-reduce.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => 0; 4 | // Use Array.prototype.reduce method 5 | // to calculate sum of all given arguments 6 | // For example sum(1, 2, 3) should return 6 7 | 8 | module.exports = { sum }; 9 | -------------------------------------------------------------------------------- /Solutions/1-for.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | let value = 0; 5 | for (let i = 0; i < args.length; i++) { 6 | value += args[i]; 7 | } 8 | return value; 9 | }; 10 | 11 | module.exports = { sum }; 12 | -------------------------------------------------------------------------------- /JavaScript/7-break.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const flag = false; 4 | 5 | label1: { 6 | console.log(1); 7 | label2: { 8 | console.log(2); 9 | break label1; 10 | console.log(3); 11 | } 12 | console.log(4); 13 | } 14 | console.log(5); 15 | -------------------------------------------------------------------------------- /Exercises/1-for.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | // Use for loop and accumulator variable 5 | // to calculate sum of all given arguments 6 | // For example sum(1, 2, 3) should return 6 7 | }; 8 | 9 | module.exports = { sum }; 10 | -------------------------------------------------------------------------------- /Exercises/6-matrix.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const max = (matrix) => { 4 | // Use nested for loop to find max value in 2d matrix 5 | // For example max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 6 | // should return 9 7 | }; 8 | 9 | module.exports = { max }; 10 | -------------------------------------------------------------------------------- /Exercises/2-for-of.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | // Use for..of loop and accumulator variable 5 | // to calculate sum of all given arguments 6 | // For example sum(1, 2, 3) should return 6 7 | }; 8 | 9 | module.exports = { sum }; 10 | -------------------------------------------------------------------------------- /Exercises/3-while.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | // Use while loop and accumulator variable 5 | // to calculate sum of all given arguments 6 | // For example sum(1, 2, 3) should return 6 7 | }; 8 | 9 | module.exports = { sum }; 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | end_of_line = lf 6 | charset = utf-8 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | 10 | [{*.js,*.mjs,*.ts,*.json,*.yml}] 11 | indent_size = 2 12 | indent_style = space 13 | -------------------------------------------------------------------------------- /Exercises/4-do-while.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | // Use do..while loop and accumulator variable 5 | // to calculate sum of all given arguments 6 | // For example sum(1, 2, 3) should return 6 7 | }; 8 | 9 | module.exports = { sum }; 10 | -------------------------------------------------------------------------------- /Solutions/4-do-while.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sum = (...args) => { 4 | if (args.length === 0) return 0; 5 | let value = 0; 6 | do { 7 | value += args.pop(); 8 | } while (args.length > 0); 9 | return value; 10 | }; 11 | 12 | module.exports = { sum }; 13 | -------------------------------------------------------------------------------- /Solutions/7-ages.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ages = (persons) => { 4 | const data = {}; 5 | for (const name in persons) { 6 | const person = persons[name]; 7 | data[name] = person.died - person.born; 8 | } 9 | return data; 10 | }; 11 | 12 | module.exports = { ages }; 13 | -------------------------------------------------------------------------------- /JavaScript/c-matrix-each.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const matrix = [ 4 | [7, 10, 1, 5, 2], 5 | [6, -1, 7, 2, 3], 6 | [1, 2, 4, -8, 2], 7 | [-6, 4, 8, 2, 0], 8 | ]; 9 | 10 | matrix.forEach((row, i) => { 11 | row.forEach((col, j) => { 12 | console.log(i, j, col); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Different implementation of iterations as a code abstraction 2 | 3 | [![Массивы, объекты, классы, прототипы](https://img.youtube.com/vi/VBMGnAPfmsY/0.jpg)](https://www.youtube.com/watch?v=VBMGnAPfmsY) 4 | [![Итерирование, циклы и итераторы](https://img.youtube.com/vi/lq3b5_UGJas/0.jpg)](https://www.youtube.com/watch?v=lq3b5_UGJas) 5 | -------------------------------------------------------------------------------- /Exercises/6-matrix.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'max', 3 | length: [220, 300], 4 | cases: [ 5 | [[[10]], 10], 6 | [[[1, 2], [3, 4], [5, 6]], 6], 7 | [[[-1, 1], [2, -1], [-1, 0]], 2], 8 | ], 9 | test: max => { 10 | const src = max.toString(); 11 | if (!src.includes('for (')) throw new Error('Use for loop'); 12 | } 13 | }) 14 | -------------------------------------------------------------------------------- /JavaScript/d-matrix-for-in.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const matrix = [ 4 | [7, 10, 1, 5, 2], 5 | [6, -1, 7, 2, 3], 6 | [1, 2, 4, -8, 2], 7 | [-6, 4, 8, 2, 0], 8 | ]; 9 | 10 | for (const i in matrix) { 11 | const row = matrix[i]; 12 | for (const j in row) { 13 | const col = row[j]; 14 | console.log(i, j, col); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "private": true, 4 | "version": "1.1.0", 5 | "author": "Timur Shemsedinov ", 6 | "license": "MIT", 7 | "scripts": { 8 | "test": "eslint ./Exercises; hpw" 9 | }, 10 | "dependencies": { 11 | "eslint": "^8.57.1", 12 | "hpw": "^0.2.4" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /JavaScript/g-reverse.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const arr = [2, 5, -1, 7, 0]; 4 | 5 | arr[Symbol.iterator] = function() { 6 | let index = this.length; 7 | return { 8 | next: () => ({ 9 | done: index-- === 0, 10 | value: this[index] 11 | }) 12 | }; 13 | }; 14 | 15 | for (const number of arr) { 16 | console.log(number); 17 | } 18 | -------------------------------------------------------------------------------- /JavaScript/9-forEach.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const numbers = [7, 10, 1, 5, 2]; 4 | numbers.forEach((item, i, arr) => { 5 | console.log(i, arr, item); 6 | }); 7 | 8 | [7, 10, 1].forEach((x) => { 9 | console.log(x); 10 | }); 11 | 12 | [7, 10, 1].forEach((x) => console.log(x)); 13 | 14 | const log = (x) => console.log(x); 15 | 16 | [7, 10, 1].forEach(log); 17 | -------------------------------------------------------------------------------- /Solutions/6-matrix.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const max = (matrix) => { 4 | let value = matrix[0][0]; 5 | for (let i = 0; i < matrix.length; i++) { 6 | const row = matrix[i]; 7 | for (let j = 0; j < row.length; j++) { 8 | const cell = row[j]; 9 | if (value < cell) value = cell; 10 | } 11 | } 12 | return value; 13 | }; 14 | 15 | module.exports = { max }; 16 | -------------------------------------------------------------------------------- /JavaScript/1-for.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | console.log('Example 1'); 4 | 5 | for (let i = 0; i < 3; i++) { 6 | console.log(i); 7 | } 8 | 9 | console.log('Example 2'); 10 | 11 | { 12 | let i = 0; 13 | for (; i < 3; i++) { 14 | console.log(i); 15 | } 16 | } 17 | 18 | console.log('Example 3'); 19 | 20 | { 21 | for (let i = 0; i < 3;) { 22 | console.log(i++); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /JavaScript/e-reduce.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | let count = 0; 4 | const arr = [7, 10, 1, 5, 2]; 5 | const sum = (acc, val) => (count++, acc + val); 6 | const res = arr.reduce(sum); 7 | console.log({ res, count }); 8 | 9 | const reduce = (fn, acc, [cur, ...rest]) => ( 10 | cur === undefined ? acc : reduce(fn, fn(acc, cur), rest) 11 | ); 12 | 13 | const res2 = reduce(sum, 0, arr); 14 | console.log({ res2 }); 15 | -------------------------------------------------------------------------------- /Exercises/5-reduce.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'sum', 3 | length: [40, 60], 4 | cases: [ 5 | [1, 2, 3, 6], 6 | [0, 0], 7 | [0], 8 | [1, -1, 1, 1], 9 | [10, -1, -1, -1, 7], 10 | ], 11 | test: sum => { 12 | const src = sum.toString(); 13 | if (!src.includes('.reduce(')) throw new Error('Use reduce method'); 14 | if (src.includes('return')) throw new Error('Do not use return'); 15 | } 16 | }) 17 | -------------------------------------------------------------------------------- /JavaScript/b-matrix.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const matrix = [ 4 | [7, 10, 1, 5, 2], 5 | [6, -1, 7, 2, 3], 6 | [1, 2, 4, -8, 2], 7 | [-6, 4, 8, 2, 0], 8 | ]; 9 | 10 | const max = (a, b) => (a > b ? a : b); 11 | 12 | const res = matrix 13 | .map((row) => row.reduce(max)) 14 | .reduce((acc, rowMax) => acc + rowMax); 15 | 16 | console.log(res); 17 | 18 | for (const row of matrix) { 19 | for (const item of row) { 20 | console.log(item); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Exercises/1-for.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'sum', 3 | length: [70, 130], 4 | cases: [ 5 | [1, 2, 3, 6], 6 | [0, 0], 7 | [0], 8 | [1, -1, 1, 1], 9 | [10, -1, -1, -1, 7], 10 | ], 11 | test: sum => { 12 | const src = sum.toString(); 13 | if (!src.includes('for (')) throw new Error('Use for loop'); 14 | if (!src.includes('for (let')) throw new Error('Use let for accumulator'); 15 | if (!src.includes('return')) throw new Error('Use return'); 16 | } 17 | }) 18 | -------------------------------------------------------------------------------- /JavaScript/5-for-in-array.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const numbers = [7, 10, 1, 5, 2]; 4 | numbers.field2 = 'Value2'; 5 | numbers[-10] = 'Value3'; 6 | numbers.field1 = 'Value1'; 7 | numbers[5] = 20; 8 | 9 | Object.defineProperty(numbers, 'newField', { 10 | enumerable: false, 11 | value: 'valueOfNewField', 12 | }); 13 | 14 | Object.prototype.inheritedProperty = 'inherited'; 15 | 16 | for (const i in numbers) { 17 | const value = numbers[i]; 18 | console.log(i, typeof i, value); 19 | } 20 | -------------------------------------------------------------------------------- /Exercises/3-while.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'sum', 3 | length: [100, 130], 4 | cases: [ 5 | [1, 2, 3, 6], 6 | [0, 0], 7 | [0], 8 | [1, -1, 1, 1], 9 | [10, -1, -1, -1, 7], 10 | ], 11 | test: sum => { 12 | const src = sum.toString(); 13 | if (!src.includes('while (')) throw new Error('Use while loop'); 14 | if (!src.includes('let')) throw new Error('Use let to define accumulator'); 15 | if (!src.includes('return')) throw new Error('Use return'); 16 | } 17 | }) 18 | -------------------------------------------------------------------------------- /Exercises/7-ages.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ages = (persons) => { 4 | // Use for..in to calculate age for each person 5 | // For example ages({ 6 | // lenin: { born: 1870, died: 1924 }, 7 | // mao: { born: 1893, died: 1976 }, 8 | // gandhi: { born: 1869, died: 1948 }, 9 | // hirohito: { born: 1901, died: 1989 }, 10 | // }) 11 | // should return { 12 | // lenin: 54, 13 | // mao: 83, 14 | // gandhi: 79, 15 | // hirohito: 88, 16 | // } 17 | }; 18 | 19 | module.exports = { ages }; 20 | -------------------------------------------------------------------------------- /JavaScript/4-for-in-obj.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const hash = { 4 | first: 7, 5 | second: 10, 6 | third: 1, 7 | fourth: 5, 8 | }; 9 | 10 | hash.fifth = 2; 11 | 12 | Object.defineProperty(hash, 'newField', { 13 | enumerable: false, 14 | value: 'valueOfNewField', 15 | }); 16 | 17 | Object.prototype.inheritedProperty = 'inherited'; 18 | 19 | for (const key in hash) { 20 | const value = hash[key]; 21 | console.log( 22 | key, '\t', typeof key, '\t', 23 | value, '\t', typeof value 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /Exercises/4-do-while.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'sum', 3 | length: [130, 160], 4 | cases: [ 5 | [1, 2, 3, 6], 6 | [0, 0], 7 | [0], 8 | [1, -1, 1, 1], 9 | [10, -1, -1, -1, 7], 10 | ], 11 | test: sum => { 12 | const src = sum.toString(); 13 | if (!src.includes('do {') || !src.includes('} while (')) throw new Error('Use do...while loop'); 14 | if (!src.includes('let')) throw new Error('Use let to define accumulator'); 15 | if (!src.includes('return')) throw new Error('Use return'); 16 | } 17 | }) 18 | -------------------------------------------------------------------------------- /JavaScript/a-map.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const log = (s, i) => { 4 | console.log(i, s); 5 | return s; 6 | }; 7 | 8 | const f1 = (x) => x * 2; 9 | const f2 = (x) => ++x; 10 | 11 | const compose = (...funcs) => (x) => funcs.reduce((v, f) => f(v), x); 12 | 13 | const f3 = compose(f1, f2); 14 | 15 | const res1 = [7, 10, 1, 5, 2] 16 | .filter((x) => x > 4) 17 | .map(f3) 18 | .reduce((acc, val) => acc + val); 19 | 20 | console.log(res1); 21 | console.log(); 22 | 23 | [7, 10, 1, 5, 2] 24 | .map(log) 25 | .map((x) => x * 2) 26 | .map(log) 27 | .map((x) => ++x) 28 | .map(log); 29 | -------------------------------------------------------------------------------- /Exercises/2-for-of.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'sum', 3 | length: [70, 130], 4 | cases: [ 5 | [1, 2, 3, 6], 6 | [0, 0], 7 | [0], 8 | [1, -1, 1, 1], 9 | [10, -1, -1, -1, 7], 10 | ], 11 | test: sum => { 12 | const src = sum.toString(); 13 | if (!src.includes('for (')) throw new Error('Use for..of loop'); 14 | if (!src.includes(' of ')) throw new Error('Use for..of in loop'); 15 | if (!src.includes('for (const')) throw new Error('Use const in loop'); 16 | if (!src.includes('let')) throw new Error('Use let to define accumulator'); 17 | if (!src.includes('return')) throw new Error('Use return'); 18 | } 19 | }) 20 | -------------------------------------------------------------------------------- /Exercises/7-ages.test: -------------------------------------------------------------------------------- 1 | ({ 2 | name: 'ages', 3 | length: [150, 190], 4 | cases: [ 5 | [ 6 | { 7 | lenin: { born: 1870, died: 1924 }, 8 | mao: { born: 1893, died: 1976 }, 9 | gandhi: { born: 1869, died: 1948 }, 10 | hirohito: { born: 1901, died: 1989 }, 11 | }, { 12 | lenin: 54, 13 | mao: 83, 14 | gandhi: 79, 15 | hirohito: 88, 16 | } 17 | ] 18 | ], 19 | test: ages => { 20 | const src = ages.toString(); 21 | if (!src.includes('for (')) throw new Error('Use for..in loop'); 22 | if (!src.includes(' in ')) throw new Error('Use for..in loop'); 23 | } 24 | }) 25 | -------------------------------------------------------------------------------- /JavaScript/h-async.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const range = { 4 | start: 1, 5 | end: 1000, 6 | [Symbol.asyncIterator]() { 7 | let value = this.start; 8 | return { 9 | next: () => Promise.resolve({ 10 | value, 11 | done: value++ === this.end + 1 12 | }) 13 | }; 14 | } 15 | }; 16 | 17 | console.dir({ 18 | range, 19 | names: Object.getOwnPropertyNames(range), 20 | symbols: Object.getOwnPropertySymbols(range), 21 | }); 22 | 23 | let k = 0; 24 | 25 | const timer = setInterval(() => { 26 | console.log('next ', k++); 27 | }, 10); 28 | 29 | (async () => { 30 | for await (const number of range) { 31 | console.log(number); 32 | } 33 | clearInterval(timer); 34 | })(); 35 | -------------------------------------------------------------------------------- /JavaScript/f-iterator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const range = { 4 | start: 1, 5 | end: 10, 6 | [Symbol.iterator]() { 7 | let value = this.start; 8 | return { 9 | next: () => ({ 10 | value, 11 | done: value++ === this.end + 1 12 | }) 13 | }; 14 | } 15 | }; 16 | 17 | console.dir({ 18 | range, 19 | names: Object.getOwnPropertyNames(range), 20 | symbols: Object.getOwnPropertySymbols(range), 21 | }); 22 | 23 | for (const number of range) { 24 | console.log(number); 25 | } 26 | 27 | const sum = (prev, cur) => prev + cur; 28 | const sumIterable = (...iterable) => iterable.reduce(sum); 29 | 30 | const sumRange = sumIterable(...range); 31 | console.log('sumRange:', sumRange); 32 | -------------------------------------------------------------------------------- /JavaScript/6-for-of.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const numbers = [7, 10, 1, 5, 2]; 4 | numbers.field2 = 'Value2'; 5 | numbers[-10] = 'Value3'; 6 | numbers.field1 = 'Value1'; 7 | numbers[5] = 20; 8 | 9 | for (const value of numbers) { 10 | console.log(value, typeof value); 11 | } 12 | 13 | const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 14 | arr[Symbol.iterator] = function() { 15 | let index = 0; 16 | const step = 2; 17 | return { 18 | next: () => { 19 | const result = { 20 | value: this[index], 21 | done: false 22 | }; 23 | if (index >= this.length) { 24 | result.done = true; 25 | return result; 26 | } 27 | index += step; 28 | return result; 29 | } 30 | }; 31 | }; 32 | 33 | for (const value of arr) { 34 | console.log(value); 35 | } 36 | -------------------------------------------------------------------------------- /JavaScript/i-async.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const range = { 4 | start: 1, 5 | end: 1000, 6 | [Symbol.asyncIterator]() { 7 | let value = this.start; 8 | return { 9 | next: () => new Promise((resolve, reject) => { 10 | setTimeout(() => { 11 | resolve({ 12 | value, 13 | done: value++ === this.end + 1 14 | }); 15 | }, 0); 16 | }) 17 | }; 18 | } 19 | }; 20 | 21 | console.dir({ 22 | range, 23 | names: Object.getOwnPropertyNames(range), 24 | symbols: Object.getOwnPropertySymbols(range), 25 | }); 26 | 27 | let k = 0; 28 | 29 | const timer = setInterval(() => { 30 | console.log('next ', k++); 31 | }, 10); 32 | 33 | (async () => { 34 | for await (const number of range) { 35 | console.log(number); 36 | } 37 | clearInterval(timer); 38 | })(); 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2024 How.Programming.Works contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Exercises.ru.md: -------------------------------------------------------------------------------- 1 | # Упражнения 2 | 3 | ## Итерирование циклами 4 | 5 | Реализуйте функцию `sum(...args)`, которая суммирует все свои аргументы, пятью 6 | разными способами. Примеры вызовов с результатами: 7 | ```js 8 | const a = sum(1, 2, 3) // a === 6 9 | const b = sum(0) // b === 0 10 | const c = sum() // c === 0 11 | const d = sum(1, -1, 1) // d === 1 12 | const e = sum(10, -1, -1, -1) // e === 7 13 | ``` 14 | 15 | 1. Цикл `for` 16 | 2. Цикл `for..of` 17 | 3. Цикл `while` 18 | 4. Цикл `do..while` 19 | 5. Метод `Array.prototype.reduce()` 20 | 21 | ## Итерирование по двумерному массиву 22 | 23 | 6. Найдите максимальный элемент в двумерном массиве 24 | ```js 25 | const m = max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); 26 | console.log(m); // 9 27 | ``` 28 | 29 | ## Итерирование объектов-справочников 30 | 31 | 7. При помощи цикла `for..in` перебрать объект-справочник с датами рождения и 32 | смерти людей и вернуть справочник с продолжительностью их жизни. Например: 33 | ```js 34 | const persons = { 35 | lenin: { born: 1870, died: 1924 }, 36 | mao: { born: 1893, died: 1976 }, 37 | gandhi: { born: 1869, died: 1948 }, 38 | hirohito: { born: 1901, died: 1989 }, 39 | }; 40 | console.log(ages(persons)); 41 | // { 42 | // lenin: 54, 43 | // mao: 83, 44 | // gandhi: 79, 45 | // hirohito: 88, 46 | // } 47 | ``` 48 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaVersion": "latest" 10 | }, 11 | "globals": { 12 | "BigInt": true 13 | }, 14 | "rules": { 15 | "indent": [ 16 | "error", 17 | 2 18 | ], 19 | "linebreak-style": [ 20 | "error", 21 | "unix" 22 | ], 23 | "quotes": [ 24 | "error", 25 | "single" 26 | ], 27 | "semi": [ 28 | "error", 29 | "always" 30 | ], 31 | "no-loop-func": [ 32 | "error" 33 | ], 34 | "block-spacing": [ 35 | "error", 36 | "always" 37 | ], 38 | "camelcase": [ 39 | "error" 40 | ], 41 | "eqeqeq": [ 42 | "error", 43 | "always" 44 | ], 45 | "strict": [ 46 | "error", 47 | "global" 48 | ], 49 | "brace-style": [ 50 | "error", 51 | "1tbs", 52 | { 53 | "allowSingleLine": true 54 | } 55 | ], 56 | "comma-style": [ 57 | "error", 58 | "last" 59 | ], 60 | "comma-spacing": [ 61 | "error", 62 | { 63 | "before": false, 64 | "after": true 65 | } 66 | ], 67 | "eol-last": [ 68 | "error" 69 | ], 70 | "func-call-spacing": [ 71 | "error", 72 | "never" 73 | ], 74 | "key-spacing": [ 75 | "error", 76 | { 77 | "beforeColon": false, 78 | "afterColon": true, 79 | "mode": "minimum" 80 | } 81 | ], 82 | "keyword-spacing": [ 83 | "error", 84 | { 85 | "before": true, 86 | "after": true, 87 | "overrides": { 88 | "function": { 89 | "after": false 90 | } 91 | } 92 | } 93 | ], 94 | "max-len": [ 95 | "error", 96 | { 97 | "code": 80, 98 | "ignoreUrls": true 99 | } 100 | ], 101 | "max-nested-callbacks": [ 102 | "error", 103 | { 104 | "max": 7 105 | } 106 | ], 107 | "new-cap": [ 108 | "error", 109 | { 110 | "newIsCap": true, 111 | "capIsNew": false, 112 | "properties": true 113 | } 114 | ], 115 | "new-parens": [ 116 | "error" 117 | ], 118 | "no-lonely-if": [ 119 | "error" 120 | ], 121 | "no-trailing-spaces": [ 122 | "error" 123 | ], 124 | "no-unneeded-ternary": [ 125 | "error" 126 | ], 127 | "no-whitespace-before-property": [ 128 | "error" 129 | ], 130 | "object-curly-spacing": [ 131 | "error", 132 | "always" 133 | ], 134 | "operator-assignment": [ 135 | "error", 136 | "always" 137 | ], 138 | "operator-linebreak": [ 139 | "error", 140 | "after" 141 | ], 142 | "semi-spacing": [ 143 | "error", 144 | { 145 | "before": false, 146 | "after": true 147 | } 148 | ], 149 | "space-before-blocks": [ 150 | "error", 151 | "always" 152 | ], 153 | "space-before-function-paren": [ 154 | "error", 155 | { 156 | "anonymous": "never", 157 | "named": "never", 158 | "asyncArrow": "always" 159 | } 160 | ], 161 | "space-in-parens": [ 162 | "error", 163 | "never" 164 | ], 165 | "space-infix-ops": [ 166 | "error" 167 | ], 168 | "space-unary-ops": [ 169 | "error", 170 | { 171 | "words": true, 172 | "nonwords": false, 173 | "overrides": { 174 | "typeof": false 175 | } 176 | } 177 | ], 178 | "no-unreachable": [ 179 | "error" 180 | ], 181 | "no-global-assign": [ 182 | "error" 183 | ], 184 | "no-self-compare": [ 185 | "error" 186 | ], 187 | "no-unmodified-loop-condition": [ 188 | "error" 189 | ], 190 | "no-constant-condition": [ 191 | "error", 192 | { 193 | "checkLoops": false 194 | } 195 | ], 196 | "no-console": [ 197 | "off" 198 | ], 199 | "no-useless-concat": [ 200 | "error" 201 | ], 202 | "no-useless-escape": [ 203 | "error" 204 | ], 205 | "no-shadow-restricted-names": [ 206 | "error" 207 | ], 208 | "no-use-before-define": [ 209 | "error", 210 | { 211 | "functions": false 212 | } 213 | ], 214 | "arrow-parens": [ 215 | "error", 216 | "always" 217 | ], 218 | "arrow-body-style": [ 219 | "error", 220 | "as-needed" 221 | ], 222 | "arrow-spacing": [ 223 | "error" 224 | ], 225 | "no-confusing-arrow": [ 226 | "error", 227 | { 228 | "allowParens": true 229 | } 230 | ], 231 | "no-useless-computed-key": [ 232 | "error" 233 | ], 234 | "no-useless-rename": [ 235 | "error" 236 | ], 237 | "no-var": [ 238 | "error" 239 | ], 240 | "object-shorthand": [ 241 | "error", 242 | "always" 243 | ], 244 | "prefer-arrow-callback": [ 245 | "error" 246 | ], 247 | "prefer-const": [ 248 | "error" 249 | ], 250 | "prefer-numeric-literals": [ 251 | "error" 252 | ], 253 | "prefer-rest-params": [ 254 | "error" 255 | ], 256 | "prefer-spread": [ 257 | "error" 258 | ], 259 | "rest-spread-spacing": [ 260 | "error", 261 | "never" 262 | ], 263 | "template-curly-spacing": [ 264 | "error", 265 | "never" 266 | ] 267 | } 268 | } 269 | --------------------------------------------------------------------------------