diff --git a/Exercises.ru.md b/Exercises.ru.md index 10b8fba..7a0acaa 100644 --- a/Exercises.ru.md +++ b/Exercises.ru.md @@ -14,7 +14,7 @@ отдает массив нечетных чисел из диапазона [15, 30] включая крайние числа. ## Функции - +~~~~ 6. Вложенные вызовы функций в цикле - Реализуйте функцию `average` с сигнатурой `average(a: number, b: number): number` вычисляющую среднее арифметическое своих diff --git a/Exercises/1-let.js b/Exercises/1-let.js index d705443..7f90c94 100644 --- a/Exercises/1-let.js +++ b/Exercises/1-let.js @@ -1,5 +1,7 @@ 'use strict'; +// +let name = 'Don Kihot'; +// +module.exports = { name }; -let name = undefined; -module.exports = { name }; diff --git a/Exercises/2-const.js b/Exercises/2-const.js index 5512738..728f4f5 100644 --- a/Exercises/2-const.js +++ b/Exercises/2-const.js @@ -1,5 +1,5 @@ 'use strict'; -const year = undefined; +const year = 1917; module.exports = { year }; diff --git a/Exercises/3-hello.js b/Exercises/3-hello.js index a597391..64e0ae3 100644 --- a/Exercises/3-hello.js +++ b/Exercises/3-hello.js @@ -1,5 +1,5 @@ 'use strict'; -const hello = null; +const hello = name => console.log(`Hello, ${name}!`); module.exports = { hello }; diff --git a/Exercises/4-range.js b/Exercises/4-range.js index 31bd852..85c0c92 100644 --- a/Exercises/4-range.js +++ b/Exercises/4-range.js @@ -1,5 +1,18 @@ 'use strict'; -const range = null; + +const range = (start, end) => { + const arr = []; + const length = end - start; + + for (let i = 0; i <= length; i++) { + arr[i] = start; + start++; + } + + return arr; +}; + + module.exports = { range }; diff --git a/Exercises/5-range-odd.js b/Exercises/5-range-odd.js index 54bb5b4..7cb8431 100644 --- a/Exercises/5-range-odd.js +++ b/Exercises/5-range-odd.js @@ -1,5 +1,16 @@ 'use strict'; -const rangeOdd = null; +const rangeOdd = (begin, end) => { + const len = Math.ceil((end - begin) / 2); + if (len < 0) return []; + const array = new Array(len); + let i = 0; + for (let n = begin; n <= end; n++) { + if (n % 2 !== 0) array[i++] = n; + } + return array; +}; + module.exports = { rangeOdd }; + diff --git a/Exercises/6-calculate.js b/Exercises/6-calculate.js index dfecf6b..e61b3e3 100644 --- a/Exercises/6-calculate.js +++ b/Exercises/6-calculate.js @@ -1,11 +1,19 @@ 'use strict'; -const square = null; +const square = n => n * n; -const cube = null; +const cube = n => n ** 3; -const average = null; +const average = (a, b) => (a + b) / 2; + +const calculate = () => { + const arr = []; + for (let i = 0; i <= 9; i++) { + const calc = average(square(i), cube(i)); + arr.push(calc); + } + return arr; +}; -const calculate = null; module.exports = { square, cube, average, calculate }; diff --git a/Exercises/7-objects.js b/Exercises/7-objects.js index 0920026..f19f3d1 100644 --- a/Exercises/7-objects.js +++ b/Exercises/7-objects.js @@ -1,5 +1,15 @@ 'use strict'; -const fn = null; +const fn = () => { + const name1 = { name: 'John' }; + let name2 = { name: 'John' }; + + name1.name = 'Dou'; + name2.name = 'Dou'; + + name2 = { name: 'John Dou' }; +}; + + module.exports = { fn }; diff --git a/Exercises/8-create.js b/Exercises/8-create.js index ac27ddd..32b9b08 100644 --- a/Exercises/8-create.js +++ b/Exercises/8-create.js @@ -1,5 +1,8 @@ 'use strict'; -const createUser = null; +const createUser = (name, city) => ({ name, city }); + +console.log(createUser('John Dou', 'Rome')); + module.exports = { createUser }; diff --git a/Exercises/9-array.js b/Exercises/9-array.js index 466c69a..f061d71 100644 --- a/Exercises/9-array.js +++ b/Exercises/9-array.js @@ -1,7 +1,13 @@ 'use strict'; -const phonebook = null; +const phonebook = [ + { name: 'Marcus Aurelius', phone: '+380445554-433' }, + { name: 'Mao Zedong', phone: '+380445554-722' }, + { name: 'Rene Descartes', phone: '+380445554-988' }, +]; -const findPhoneByName = null; +const findPhoneByName = name => { + for (const number of phonebook) number.name === name ? number.phone : null; +}; module.exports = { phonebook, findPhoneByName }; diff --git a/Exercises/a-hash.js b/Exercises/a-hash.js index 466c69a..0dcfe98 100644 --- a/Exercises/a-hash.js +++ b/Exercises/a-hash.js @@ -1,7 +1,11 @@ 'use strict'; -const phonebook = null; +const phonebook = { + MarcusAurelius: { phone: '+380445554-433' }, + MaoZedong: { phone: '+380445554-722' }, + ReneDescartes: { phone: '+380445554-988' }, +}; -const findPhoneByName = null; +const findPhoneByName = name => phonebook[name]; module.exports = { phonebook, findPhoneByName }; diff --git a/Solutions/1-let.js b/Solutions/1-let.js deleted file mode 100644 index ef652cf..0000000 --- a/Solutions/1-let.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -let name = 'Marcus'; - -module.exports = { name }; diff --git a/Solutions/2-const.js b/Solutions/2-const.js deleted file mode 100644 index a97a674..0000000 --- a/Solutions/2-const.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -const year = 1980; - -module.exports = { year }; diff --git a/Solutions/3-hello.js b/Solutions/3-hello.js deleted file mode 100644 index 4fd09c3..0000000 --- a/Solutions/3-hello.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -const hello = name => { - console.log(`Hello, ${name}!`); -}; - -module.exports = { hello }; diff --git a/Solutions/4-range.js b/Solutions/4-range.js deleted file mode 100644 index e3fffec..0000000 --- a/Solutions/4-range.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -const range = (begin, end) => { - const len = end - begin; - if (len < 0) return []; - const array = new Array(len); - let i = 0; - for (let n = begin; n <= end; n++) { - array[i++] = n; - } - return array; -}; - -module.exports = { range }; diff --git a/Solutions/5-range-odd.js b/Solutions/5-range-odd.js deleted file mode 100644 index 908eedb..0000000 --- a/Solutions/5-range-odd.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -const rangeOdd = (begin, end) => { - const len = Math.ceil((end - begin) / 2); - if (len < 0) return []; - const array = new Array(len); - let i = 0; - for (let n = begin; n <= end; n++) { - if (n % 2 !== 0) array[i++] = n; - } - return array; -}; - -module.exports = { rangeOdd }; diff --git a/Solutions/6-calculate.js b/Solutions/6-calculate.js deleted file mode 100644 index e8c8f29..0000000 --- a/Solutions/6-calculate.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -const square = x => x * x; - -const cube = x => x ** 3; - -const average = (a, b) => (a + b) / 2; - -const calculate = () => { - const array = []; - for (let i = 0; i <= 9; i++) { - const x = average(square(i), cube(i)); - array.push(x); - } - return array; -}; - -module.exports = { square, cube, average, calculate }; diff --git a/Solutions/7-objects.js b/Solutions/7-objects.js deleted file mode 100644 index cfb23dd..0000000 --- a/Solutions/7-objects.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -const fn = () => { - const obj1 = { name: 'Marcus' }; - let obj2 = { name: 'Marcus' }; - - obj1.name = 'Aurelius'; - obj2.name = 'Aurelius'; - - obj2 = { name: 'Marcus Aurelius' }; -}; - -module.exports = { fn }; diff --git a/Solutions/8-create.js b/Solutions/8-create.js deleted file mode 100644 index f400582..0000000 --- a/Solutions/8-create.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -const createUser = (name, city) => ({ name, city }); - -module.exports = { createUser }; diff --git a/Solutions/9-array.js b/Solutions/9-array.js deleted file mode 100644 index 1ba26e1..0000000 --- a/Solutions/9-array.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -const phonebook = [ - { name: 'Marcus', phone: '+380445554433' }, - { name: 'Timur', phone: '+380661874632' }, -]; - -const findPhoneByName = name => { - for (const obj of phonebook) { - if (obj.name === name) return obj.phone; - } -}; - -module.exports = { phonebook, findPhoneByName }; diff --git a/Solutions/a-hash.js b/Solutions/a-hash.js deleted file mode 100644 index f3f13e2..0000000 --- a/Solutions/a-hash.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -const phonebook = { - Marcus: '+380445554433', - Timur: '+380661874632', -}; - -const findPhoneByName = name => phonebook[name]; - -module.exports = { phonebook, findPhoneByName };