From b84853c8080e7963b16cd1cb5778e704836eaf76 Mon Sep 17 00:00:00 2001 From: vladimirvikulin Date: Wed, 22 Sep 2021 16:37:25 +0300 Subject: [PATCH] Lab2 Reusable --- .eslintrc.json | 2 +- Exercises/1-let.js | 2 +- Exercises/2-const.js | 2 +- Exercises/3-hello.js | 4 +++- Exercises/4-range.js | 9 ++++++++- Exercises/5-range-odd.js | 10 +++++++++- Exercises/6-calculate.js | 15 +++++++++++---- Exercises/7-objects.js | 12 +++++++++++- Exercises/8-create.js | 2 +- Exercises/9-array.js | 13 +++++++++++-- Exercises/a-hash.js | 8 ++++++-- package.json | 2 +- 12 files changed, 64 insertions(+), 17 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 7bb3f20..1deea4a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,7 @@ ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", diff --git a/Exercises/1-let.js b/Exercises/1-let.js index d705443..441a467 100644 --- a/Exercises/1-let.js +++ b/Exercises/1-let.js @@ -1,5 +1,5 @@ 'use strict'; -let name = undefined; +let name = 'Vladimir'; module.exports = { name }; diff --git a/Exercises/2-const.js b/Exercises/2-const.js index 5512738..9c247d3 100644 --- a/Exercises/2-const.js +++ b/Exercises/2-const.js @@ -1,5 +1,5 @@ 'use strict'; -const year = undefined; +const year = 2004; module.exports = { year }; diff --git a/Exercises/3-hello.js b/Exercises/3-hello.js index a597391..d9bde71 100644 --- a/Exercises/3-hello.js +++ b/Exercises/3-hello.js @@ -1,5 +1,7 @@ '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..ebdd706 100644 --- a/Exercises/4-range.js +++ b/Exercises/4-range.js @@ -1,5 +1,12 @@ 'use strict'; -const range = null; +const range = (start, end) => { + const array = []; + if (array.length < 0) return []; + for (let i = start; i <= end; i++) { + array.push(i); + } + return array; +}; module.exports = { range }; diff --git a/Exercises/5-range-odd.js b/Exercises/5-range-odd.js index 54bb5b4..0ff07cf 100644 --- a/Exercises/5-range-odd.js +++ b/Exercises/5-range-odd.js @@ -1,5 +1,13 @@ 'use strict'; -const rangeOdd = null; +const rangeOdd = (start, end) => { + const array = []; + if (array.length < 0) return []; + for (let i = start; i <= end; i++) { + if (i % 2 !== 0) + array.push(i); + } + return array; +}; module.exports = { rangeOdd }; diff --git a/Exercises/6-calculate.js b/Exercises/6-calculate.js index dfecf6b..d0f7e1f 100644 --- a/Exercises/6-calculate.js +++ b/Exercises/6-calculate.js @@ -1,11 +1,18 @@ 'use strict'; -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 array = []; + for (let i = 0; i <= 9; i++) { + const ave = average(square(i), cube(i)); + array.push(ave); + } + return array; +}; module.exports = { square, cube, average, calculate }; diff --git a/Exercises/7-objects.js b/Exercises/7-objects.js index 0920026..8373a35 100644 --- a/Exercises/7-objects.js +++ b/Exercises/7-objects.js @@ -1,5 +1,15 @@ 'use strict'; -const fn = null; +const fn = () => { + let objectLet = { name: 'Vladimir' }; + const objectConst = { name: 'Vladimir' }; + objectLet.name = 'Sergey'; + objectConst.name = 'Sergey'; + const object = { name: 'Timur' }; + objectLet = object; + //objectConst = object; - TypeError: Assignment to constant variable +}; + +fn(); module.exports = { fn }; diff --git a/Exercises/8-create.js b/Exercises/8-create.js index ac27ddd..f400582 100644 --- a/Exercises/8-create.js +++ b/Exercises/8-create.js @@ -1,5 +1,5 @@ 'use strict'; -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 466c69a..1ba7abc 100644 --- a/Exercises/9-array.js +++ b/Exercises/9-array.js @@ -1,7 +1,16 @@ 'use strict'; -const phonebook = null; +const phonebook = [ + { name: 'John', phone: '+380669876420' }, + { name: 'Vladimir', phone: '+380959546176' }, + { name: 'Sergey', phone: '+380997865430' } +]; -const findPhoneByName = null; +const findPhoneByName = (name) => { + for (const k of phonebook) { + if (k.name === name) + return k.phone; + } +}; module.exports = { phonebook, findPhoneByName }; diff --git a/Exercises/a-hash.js b/Exercises/a-hash.js index 466c69a..6929893 100644 --- a/Exercises/a-hash.js +++ b/Exercises/a-hash.js @@ -1,7 +1,11 @@ 'use strict'; -const phonebook = null; +const phonebook = { + John: '+380669876420', + Vladimir: '+380959546176', + Sergey: '+380997865430' +}; -const findPhoneByName = null; +const findPhoneByName = (name) => phonebook[name]; module.exports = { phonebook, findPhoneByName }; diff --git a/package.json b/package.json index 147ac83..e4c1d7c 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": {