diff --git a/Exercises/1-let.js b/Exercises/1-let.js index 49c90f5..193d404 100644 --- a/Exercises/1-let.js +++ b/Exercises/1-let.js @@ -2,6 +2,6 @@ // Define variable to store your name as a string -let name = undefined; +let name = 'Mariia'; module.exports = { name }; diff --git a/Exercises/2-const.js b/Exercises/2-const.js index 3d82a41..11eba14 100644 --- a/Exercises/2-const.js +++ b/Exercises/2-const.js @@ -2,6 +2,6 @@ // Define constant to store your birth year as a number -const year = undefined; +const year = 2006; module.exports = { year }; diff --git a/Exercises/3-hello.js b/Exercises/3-hello.js index 76df912..4328fdd 100644 --- a/Exercises/3-hello.js +++ b/Exercises/3-hello.js @@ -2,6 +2,6 @@ // Prepare function to print greeting with single argument -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 3cba2d6..6126d88 100644 --- a/Exercises/4-range.js +++ b/Exercises/4-range.js @@ -3,6 +3,12 @@ // Implement function `range(start: number, end: number): array` returning // array with all numbers from the range [15, 30] including endpoints -const range = null; +const range = (start, end) => { + const result = []; + for (let i = start; i <= end; i++) { + result.push(i); + } + return result; +}; module.exports = { range }; diff --git a/Exercises/5-range-odd.js b/Exercises/5-range-odd.js index 2eab790..ab758ea 100644 --- a/Exercises/5-range-odd.js +++ b/Exercises/5-range-odd.js @@ -3,6 +3,14 @@ // Implement function `rangeOdd(start: number, end: number)` returning // array with all odd numbers from the range [15, 30] including endpoints -const rangeOdd = null; +const rangeOdd = (start, end) => { + const result = []; + for (let i = start; i <= end; i++) { + if (i % 2 !== 0) { + result.push(i); + } + } + return result; +}; module.exports = { rangeOdd }; diff --git a/Exercises/6-calculate.js b/Exercises/6-calculate.js index a6cbf31..4899a38 100644 --- a/Exercises/6-calculate.js +++ b/Exercises/6-calculate.js @@ -16,12 +16,19 @@ Call functions `square` and `cube` in loop, then pass their results to function `average`. Print what `average` returns. */ -const square = null; +const square = (a) => a * a; -const cube = null; +const cube = (a) => a ** 3; -const average = null; +const average = (a, b) => (a + b) / 2; -const calculate = null; +const calculate = () => { + const result = []; + for (let i = 0; i < 10; i++) { + const averageCalculate = average(square(i), cube(i)); + result.push(averageCalculate); + } + return result; +}; module.exports = { square, cube, average, calculate }; diff --git a/Exercises/7-objects.js b/Exercises/7-objects.js index c892718..7c41b8b 100644 --- a/Exercises/7-objects.js +++ b/Exercises/7-objects.js @@ -1,12 +1,21 @@ 'use strict'; -/* Do following tasks inside function `fn` (see stub: `7-objects.js`) -- Define constant object with single field `name`. -- Define variable object with single field `name`. -- Try to change field `name`. -- Try to assign other object to both identifiers. -- Explain script behaviour. */ - -const fn = null; +const fn = () => { + const mariia = { name: 'Mariia Khorunzha' }; + let rachel = { name: 'Rachel Amber' }; + + mariia.name = 'Oksana'; + rachel.name = 'Chloe'; + + rachel = { name: 'Max Caulfield' }; + +}; + +// mariia = { name: 'Marusya Churay' } TypeError: Assignment +// to constant variable. + +// Висновок: Можливо змінювати значення полів і у константі, і у змінній, +// але змінювати посилання на об'єкт в константі неможливо. + module.exports = { fn }; diff --git a/Exercises/8-create.js b/Exercises/8-create.js index a3d2a41..42c3e32 100644 --- a/Exercises/8-create.js +++ b/Exercises/8-create.js @@ -5,6 +5,6 @@ Example: `createUser('Marcus Aurelius', 'Roma')` will return object `{ name: 'Marcus Aurelius', city: 'Roma' }` */ -const createUser = null; +const createUser = (name, city) => ({ name, city }); module.exports = { createUser }; diff --git a/Exercises/9-array.js b/Exercises/9-array.js index 1c93d90..2eb912c 100644 --- a/Exercises/9-array.js +++ b/Exercises/9-array.js @@ -9,8 +9,16 @@ Object example: `{ name: 'Marcus Aurelius', phone: '+380445554433' }`. `findPhoneByName(name: string): string`. Returning phone from that object where field `name` equals argument `name`. Use `for` loop for this search. */ -const phonebook = null; +const phonebook = [{ name: 'Marcus Aurelius', phone: '+380445554433' }, + { name: 'Volodymyr Zelenskyy', phone: '+380441112233' }, + { name: 'Lesya Ukrainka', phone: '+380443334455' }, + { name: 'Taras Shevchenko', phone: '+380446663344' }, + { name: 'Ivan Franko', phone: '+380441234567' }]; -const findPhoneByName = null; +const findPhoneByName = (name) => { + for (const i of phonebook) { + if (i.name === name) return i.phone; + } +}; module.exports = { phonebook, findPhoneByName }; diff --git a/Exercises/a-hash.js b/Exercises/a-hash.js index da97309..81f6756 100644 --- a/Exercises/a-hash.js +++ b/Exercises/a-hash.js @@ -7,8 +7,14 @@ contains `phone`. `findPhoneByName(name: string): string`. Returning phone from hash/object. Use `hash[key]` to find needed phone. */ -const phonebook = null; +const phonebook = { + 'Marcus Aurelius': { phone: '+380445554433' }, + 'Volodymyr Zelenskyy': { phone: '+380441112233' }, + 'Lesya Ukrainka': { phone: '+380443334455' }, + 'Taras Shevchenko': { phone: '+380446663344' }, + 'Ivan Franko': { phone: '+380441234567' }, +}; -const findPhoneByName = null; +const findPhoneByName = (name) => phonebook[name]; module.exports = { phonebook, findPhoneByName }; diff --git a/package.json b/package.json index fb9df45..7fcb763 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Timur Shemsedinov ", "license": "MIT", "scripts": { - "test": "eslint ./Exercises; hpw", + "test": "eslint ./Exercises && hpw", "ci": "eslint ./Exercises && hpw" }, "dependencies": {