From 35bbe492be38b7248a0b0245a1976ab69c5b9695 Mon Sep 17 00:00:00 2001 From: hok7z Date: Wed, 27 Nov 2024 22:17:54 +0200 Subject: [PATCH] First commit --- Exercises/1-let.js | 2 +- Exercises/2-const.js | 2 +- Exercises/3-hello.js | 4 +++- Exercises/4-range.js | 10 +++++++++- Exercises/5-range-odd.js | 12 +++++++++++- Exercises/6-calculate.js | 19 +++++++++++++++---- Exercises/7-objects.js | 13 ++++++++++++- Exercises/8-create.js | 2 +- Exercises/9-array.js | 12 ++++++++++-- Exercises/a-hash.js | 8 ++++++-- 10 files changed, 69 insertions(+), 15 deletions(-) diff --git a/Exercises/1-let.js b/Exercises/1-let.js index 49c90f5..90da30b 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 = 'Roman'; 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..aa17b5f 100644 --- a/Exercises/3-hello.js +++ b/Exercises/3-hello.js @@ -2,6 +2,8 @@ // 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..d8aa3fd 100644 --- a/Exercises/4-range.js +++ b/Exercises/4-range.js @@ -3,6 +3,14 @@ // 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, number) => { + const arr = []; + + for (let i = start; i <= number; i++) { + arr.push(i); + } + + return arr; +}; module.exports = { range }; diff --git a/Exercises/5-range-odd.js b/Exercises/5-range-odd.js index 2eab790..9a91cfb 100644 --- a/Exercises/5-range-odd.js +++ b/Exercises/5-range-odd.js @@ -3,6 +3,16 @@ // Implement function `rangeOdd(start: number, end: number)` returning // array with all odd numbers from the range [15, 30] including endpoints -const rangeOdd = null; +const isOdd = (a) => a % 2; + +const rangeOdd = (start, end) => { + const arr = []; + + for (let i = start; i <= end; i++) { + if (isOdd(i)) arr.push(i); + } + + return arr; +}; module.exports = { rangeOdd }; diff --git a/Exercises/6-calculate.js b/Exercises/6-calculate.js index a6cbf31..9251104 100644 --- a/Exercises/6-calculate.js +++ b/Exercises/6-calculate.js @@ -16,12 +16,23 @@ Call functions `square` and `cube` in loop, then pass their results to function `average`. Print what `average` returns. */ -const square = null; -const cube = null; +const square = (x) => x * x; -const average = null; +const cube = (x) => x ** 3; -const calculate = null; +const average = (a, b) => (a + b) / 2; + +function calculate() { + const arr = []; + + for (let i = 0; i <= 9; i++) { + const result = average(square(i), cube(i)); + // console.log(result); + arr.push(result); + } + + return arr; +} module.exports = { square, cube, average, calculate }; diff --git a/Exercises/7-objects.js b/Exercises/7-objects.js index c892718..3e7ca40 100644 --- a/Exercises/7-objects.js +++ b/Exercises/7-objects.js @@ -7,6 +7,17 @@ - Try to assign other object to both identifiers. - Explain script behaviour. */ -const fn = null; +const fn = function() { + // Init objects + const obj1 = { name: 'Linus', }; + let obj2 = { name: 'Satochi' }; + + // Just change value of field `name` + obj1.name = 'Richard'; + obj2.name = 'Vitalik'; + + // Obj1 is a variable we can change data in it + obj2 = { name: 'Lennart' }; +}; 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..0c525a8 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: 'Linus Torvalds', phone: '+38056323498' }, + { name: 'Richard Stallman', phone: '+3808384834' }, + { name: 'Eugen Rochko', phone: '+380734190748' }, +]; -const findPhoneByName = null; +const findPhoneByName = (name) => { + for (const record of phonebook) { + if (record.name === name) return record; + } +}; module.exports = { phonebook, findPhoneByName }; diff --git a/Exercises/a-hash.js b/Exercises/a-hash.js index da97309..a0f7b09 100644 --- a/Exercises/a-hash.js +++ b/Exercises/a-hash.js @@ -7,8 +7,12 @@ contains `phone`. `findPhoneByName(name: string): string`. Returning phone from hash/object. Use `hash[key]` to find needed phone. */ -const phonebook = null; +const phonebook = { + linus: '+38056323498', + richard: '+3808384834', + eugen: '+380734190748', +}; -const findPhoneByName = null; +const findPhoneByName = (name) => phonebook[name]; module.exports = { phonebook, findPhoneByName };