From b294742d72c5308b47709c68863e2b5edffdb5b8 Mon Sep 17 00:00:00 2001 From: Denis Vasilenko Date: Tue, 21 Jan 2025 21:57:48 +0300 Subject: [PATCH] All is done --- Exercises/1-let.js | 2 +- Exercises/2-const.js | 2 +- Exercises/3-hello.js | 2 +- Exercises/4-range.js | 11 ++++++++++- Exercises/5-range-odd.js | 9 ++++++++- Exercises/6-calculate.js | 14 ++++++++++---- Exercises/7-objects.js | 15 ++++++++++++++- Exercises/8-create.js | 2 +- Exercises/9-array.js | 16 ++++++++++++++-- Exercises/a-hash.js | 7 +++++-- JavaScript/2-loop.js | 2 +- 11 files changed, 66 insertions(+), 16 deletions(-) diff --git a/Exercises/1-let.js b/Exercises/1-let.js index 49c90f5..ba8097b 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 = 'Denis'; module.exports = { name }; diff --git a/Exercises/2-const.js b/Exercises/2-const.js index 3d82a41..62da4a5 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 = 1980; 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..454e051 100644 --- a/Exercises/4-range.js +++ b/Exercises/4-range.js @@ -3,6 +3,15 @@ // 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; +}; + +// const result = range(1, 10); +// console.log({ result }); module.exports = { range }; diff --git a/Exercises/5-range-odd.js b/Exercises/5-range-odd.js index 2eab790..a7480b2 100644 --- a/Exercises/5-range-odd.js +++ b/Exercises/5-range-odd.js @@ -3,6 +3,13 @@ // 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) continue; + result.push(i); + } + return result; +}; module.exports = { rangeOdd }; diff --git a/Exercises/6-calculate.js b/Exercises/6-calculate.js index a6cbf31..3619644 100644 --- a/Exercises/6-calculate.js +++ b/Exercises/6-calculate.js @@ -16,12 +16,18 @@ Call functions `square` and `cube` in loop, then pass their results to function `average`. Print what `average` returns. */ -const square = null; +const square = (x) => x * x; -const cube = null; +const cube = (x) => x ** 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++) { + result.push(average(square(i), cube(i))); + } + return result; +}; module.exports = { square, cube, average, calculate }; diff --git a/Exercises/7-objects.js b/Exercises/7-objects.js index c892718..2f0c5b7 100644 --- a/Exercises/7-objects.js +++ b/Exercises/7-objects.js @@ -7,6 +7,19 @@ - Try to assign other object to both identifiers. - Explain script behaviour. */ -const fn = null; +const fn = () => { + const obj1 = { + name: 'Denis', + }; + let obj2 = { + name: 'Milana', + }; + obj1.name = 'Vasia'; + obj2.name = 'Alisa'; + //obj1 = {}; + obj2 = {}; +}; + + 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..7211a45 100644 --- a/Exercises/9-array.js +++ b/Exercises/9-array.js @@ -9,8 +9,20 @@ 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: 'Denis Vasilenko', phone: '+79673196182' }, + { name: 'Milane Ganeeva', phone: '+78934832938' }, +]; + +const findPhoneByName = (name) => { + for (const user of phonebook) { + if (user.name === name) return user.phone; + } +}; + +console.log(findPhoneByName('Denis Vasilenko')); + -const findPhoneByName = null; module.exports = { phonebook, findPhoneByName }; diff --git a/Exercises/a-hash.js b/Exercises/a-hash.js index da97309..616e928 100644 --- a/Exercises/a-hash.js +++ b/Exercises/a-hash.js @@ -7,8 +7,11 @@ contains `phone`. `findPhoneByName(name: string): string`. Returning phone from hash/object. Use `hash[key]` to find needed phone. */ -const phonebook = null; +const phonebook = { + Marcus: '+380445554433', + Denis: '+79673196182', +}; -const findPhoneByName = null; +const findPhoneByName = (name) => phonebook[name]; module.exports = { phonebook, findPhoneByName }; diff --git a/JavaScript/2-loop.js b/JavaScript/2-loop.js index 499ebe0..c2df167 100644 --- a/JavaScript/2-loop.js +++ b/JavaScript/2-loop.js @@ -4,6 +4,6 @@ const MAX_VALUE = 10; console.log('Begin'); for (let i = 0; i < MAX_VALUE; i++) { - console.dir({ i, date: new Date() }); + console.dir({ counter: i, date: new Date() }); } console.log('The end');