From 544df52891e6eb708e33bbc6b51d196a6dcad1fe Mon Sep 17 00:00:00 2001 From: Lana Trupkina Date: Fri, 12 Mar 2021 17:12:48 +0200 Subject: [PATCH 1/6] HW5_Trupkina(draft) --- HW5(OOP_vs_FP)/HW5_Trupkina.js | 236 +++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 HW5(OOP_vs_FP)/HW5_Trupkina.js diff --git a/HW5(OOP_vs_FP)/HW5_Trupkina.js b/HW5(OOP_vs_FP)/HW5_Trupkina.js new file mode 100644 index 0000000..b455197 --- /dev/null +++ b/HW5(OOP_vs_FP)/HW5_Trupkina.js @@ -0,0 +1,236 @@ + +// Write two solutions for each problem. +// _.1 in an iterative style (no higher-order functions) +// _.2 applying higher-order functions + +// 1. +const arrBirthday = [1998, 1985, 2001, 2006]; + +//1.1 +function findAgeIn2021(arr) { + const arr2021 = []; + for (let i = 0; i < arr.length; i++) { + arr2021.push(2021 - arr[i]); + } + return arr2021; +} +console.log(findAgeIn2021(arrBirthday)); +//(4) [23, 36, 20, 15] + +//1.2 hof +//create array fhat contain current age in 2021 with hof +function findAgeIn2021_hof(arr, fn) { + const arr2021 = []; + for (let i = 0; i < arr.length; i++) { + arr2021.push(fn(arr[i]));//call special func for calculate age + } + return arr2021; +} +function findNowAge(elem) { + const ageNow = 2021 - elem; + return ageNow; +} +console.log(findAgeIn2021_hof(arrBirthday, findNowAge)); +//(4) [23, 36, 20, 15] + +//1.2.1 hof map +const arrAgeNow_hof = arrBirthday.map(function (elem) { + return elem = (2021 - elem); +}); +console.log(arrAgeNow); +//(4) [23, 36, 20, 15] + +// 2. +//2.1 +const arrPeople = [ + { name: 'Pedro Pascal', age: 45 }, + { name: 'Leonardo Dicaprio', age: 46 }, + { name: 'Thomas A. Anderson', age: 58 }, + { name: 'Millie Brown', age: 17 }, + { name: 'Harry Potter', age: 31 } +]; +function findAdults(arr) { + const arrOver18 = []; + for (let i = 0; i < arr.length; i++) { + if (arr[i].age >= 18) { + arrOver18.push(arr[i]); + } + } + return arrOver18; +} +console.log(findAdults(arrPeople)); +/*(4) [{…}, {…}, {…}, {…}] +0: {name: "Pedro Pascal", age: 45} +1: {name: "Leonardo Dicaprio", age: 46} +2: {name: "Thomas A. Anderson", age: 58} +3: {name: "Harry Potter", age: 31} */ + +//2.2 hof +function findAdults_hof(arr, fn) { + const arrOver18 = []; + for (let i = 0; i < arr.length; i++) { + if (fn(arr[i]) === undefined) i++; + arrOver18.push(fn(arr[i])); + } + return arrOver18; +} +function more18(elem) { + if (elem.age >= 18) return elem.name; +} +console.log(findAdults_hof(arrPeople, more18)); +//(4) ["Pedro Pascal", "Leonardo Dicaprio", "Thomas A. Anderson", "Harry Potter"] + +//2.2.1 hof filter +const findAdults_filter = arrPeople.filter(arrPeople => arrPeople.age >= 18); +console.log(findAdults_filter); +/*(4) [{…}, {…}, {…}, {…}] +0: {name: "Pedro Pascal", age: 45} +1: {name: "Leonardo Dicaprio", age: 46} +2: {name: "Thomas A. Anderson", age: 58} +3: {name: "Harry Potter", age: 31}*/ + + +// 3. +//3.1 +const arrNum = [34, 38, 45, 9, 23]; +function findSumArrNumb(numb) { + let sum = 0; + for (let i = 0; i < numb.length; i++) { + sum += numb[i]; + } + return sum; +} +console.log(findSumArrNumb(arrNum));//149 + + +//3.2.1 reduce________________________________ +const sum = arrNum.reduce(function (accumulator, currentValue) { //.reduce(function(accum, currentValue, index[0...n], array) { + return accumulator + currentValue; +}); +console.log(sum);//25 + +// 4. +const arrPeopleName = ['Pedro', 'Leonardo', 'Anderson', 'Millie', 'Harry']; + +function findNameLength_hof(arr, fn) { + const arrlen = []; + for (let i = 0; i < arr.length; i++) { + arrlen.push(fn(arr[i])); + } + return arrlen; +} +function retName(item) { + return item.length; +} +const arrMap = findNameLength_hof(arrPeopleName, retName); +console.log(arrMap); + + +// ______________Task for working with OOP:_______________ +// ES6 +class Company { + + constructor(options) { + this.name = options.name; + this.vacation = options.vacation; + } + startVacation() { } + finishVacation() { } +} +// Inheritance class +class PersonnelDep extends Company { + + startVacation() {//Polymorphism shows which version of method should be run + this.vacation = true; + console.log(`I'm on vacation! :D`) // + } + finishVacation() { + this.vacation = false; + console.log(`I'm at work...`); + } + + get getAdvice() {//encapsulation + let getInspirit = 'you were inspired...' + return console.log(getInspirit);//you were inspired... + + } +} + +// Inheritance class +class Students extends PersonnelDep { + constructor(options) { + super(options); + this.task = options.task; + } + //Polymorphism shows which version of method should be run + startVacation() {//New method will clear parenth if i want to call parenth method should be used super.makeVacation() + this.vacation = true; + //super.startVacation(); + console.log(`I'm on holiday! :D`); + } + finishVacation() { + this.vacation = false; + console.log(`holiday is done :(`); + } + + homeworkCheck(options) { + let needTask = 0; + this.task = options; + console.log('Need to do ' + (needTask = 14 - options) + ' task'); + } +} +const student1 = new Students({ + name: 'Dima', + task: 0, + vacation: false +}) +const student2 = new Students({ + name: 'Lana', + task: 0, + vacation: false +}) +const mentor = new PersonnelDep({ + name: 'Dmitry', + vacation: false, +}) + +student1.startVacation();// .startVacation(false) - (holiday is done :( ) +// .startVacation(true) - (I'm on holiday! :D) +student1.getAdvice(); + +// prototypes + + + +// ________ Functional programming task________ + +// Higher-order functions + +// First class functions + +// Pure functions + +// Function side effect + +// Unchanging state +// Shared State +// Closures +// Recursion +// Partial function application +//First-Class and Pure function +const timeMinut = function (min) { + return min * 60; +}; +//Higher-order Functions +function startTimer(timeMinut) { + seconds = timeMinut % 60 // got second + minutes = timeMinut / 60 % 60 // got minute + hour = timeMinut / 60 / 60 % 60 // got hour + // if time gone... + if (timeMinut <= 0) { + clearInterval(timer); + alert('Time is over.'); + } else { + // time output + let strTimer = `${Math.trunc(hour)}:${Math.trunc(minutes)}:${seconds}`; + console.log(strTimer); + } + --timeMinut; // decrement for timer +} +//timer = setInterval(, 1000); \ No newline at end of file From 66d8f1c16e680eabcd759d81d0f4f7411bb556ba Mon Sep 17 00:00:00 2001 From: Lana Trupkina Date: Mon, 15 Mar 2021 02:54:18 +0200 Subject: [PATCH 2/6] HW5_Trupkina (pre-Done(without proto)) --- HW5(OOP_vs_FP)/HW5_Trupkina.js | 143 ++++++++++++++++++++++++--------- 1 file changed, 106 insertions(+), 37 deletions(-) diff --git a/HW5(OOP_vs_FP)/HW5_Trupkina.js b/HW5(OOP_vs_FP)/HW5_Trupkina.js index b455197..a507387 100644 --- a/HW5(OOP_vs_FP)/HW5_Trupkina.js +++ b/HW5(OOP_vs_FP)/HW5_Trupkina.js @@ -110,6 +110,7 @@ const sum = arrNum.reduce(function (accumulator, currentValue) { //.reduce(funct console.log(sum);//25 // 4. +//4.1 const arrPeopleName = ['Pedro', 'Leonardo', 'Anderson', 'Millie', 'Harry']; function findNameLength_hof(arr, fn) { @@ -124,9 +125,14 @@ function retName(item) { } const arrMap = findNameLength_hof(arrPeopleName, retName); console.log(arrMap); +//4.2 +const mapNameLength = arrPeopleName.map(function (name) { + return name.length; +}); +console.log(mapNameLength);//[5, 8, 8, 6, 5] -// ______________Task for working with OOP:_______________ +// Task for working with OOP: // ES6 class Company { @@ -142,15 +148,15 @@ class PersonnelDep extends Company { startVacation() {//Polymorphism shows which version of method should be run this.vacation = true; - console.log(`I'm on vacation! :D`) // + console.log(`I'm ${this.name}! I'm on vacation! :D`) // } finishVacation() { this.vacation = false; - console.log(`I'm at work...`); + console.log(`${this.name} at work...`); } get getAdvice() {//encapsulation - let getInspirit = 'you were inspired...' + let getInspirit = `${this.name} were inspired...` return console.log(getInspirit);//you were inspired... } @@ -166,17 +172,17 @@ class Students extends PersonnelDep { startVacation() {//New method will clear parenth if i want to call parenth method should be used super.makeVacation() this.vacation = true; //super.startVacation(); - console.log(`I'm on holiday! :D`); + console.log(`I'm ${this.name}! I'm on holiday! :D`); } finishVacation() { this.vacation = false; - console.log(`holiday is done :(`); + console.log(`holiday is done :( ${this.name} at work...`); } homeworkCheck(options) { let needTask = 0; this.task = options; - console.log('Need to do ' + (needTask = 14 - options) + ' task'); + console.log('Need to do ' + (needTask = 14 - options) + ' task for ' + this.name); } } const student1 = new Students({ @@ -194,43 +200,106 @@ const mentor = new PersonnelDep({ vacation: false, }) -student1.startVacation();// .startVacation(false) - (holiday is done :( ) -// .startVacation(true) - (I'm on holiday! :D) -student1.getAdvice(); +student1.startVacation(); // I'm Dima! I'm on holiday! :D +student2.finishVacation(); //holiday is done :( Lana at work... +student2.homeworkCheck(5); //Need to do 9 task for Lana +student2.getAdvice; //Lana were inspired... +mentor.startVacation(); //I'm Dmitry! I'm on vacation! :D -// prototypes +//__________ prototypes__________ +function Company2() { //abstract class (constructor) + this.name = null; + this.vacation = false; +} +Company2.prototype.startVacation = function () { }; +Company2.prototype.finishVacation = function () { }; +function PersonnelDep2(params) { //Inheritance Class + Company2.call(this); -// ________ Functional programming task________ +} +function Students2(params) { //Inheritance Class + Company2.call(this); + this.task = params.name; + this.name = params.t; + this.vacation = params; +} +Students2.prototype.startVacation = function () { + this.vacation = true; + console.log(`I'm on holiday! :D`); +} +Students2.prototype.finishVacation = function () { + this.vacation = false; + console.log(`holiday is done :(`); +} +Students2.prototype.homeworkCheck = function () { + let needTask = 0; + this.task = options; + console.log('Need to do ' + (needTask = 14 - options) + ' task'); +} +const student1_2 = new Students2('Dima', 0, false); +const student2_2 = new Students2('Lana', 0, false); +const mentor2 = new PersonnelDep2('Dmitry', false,); + +console.log(student1_2);//Students2 {name: "Dima", vacation: "Dima", task: "Dima"} +//_________________________________________________ +// Functional programming task: +// Function side effect +function randomNumber(min, max) {// function will be returned as a result different values + return Math.floor(Math.random() * (max - min) + min); +} +console.log(randomNumber(1, 10)); -// Higher-order functions + -// First class functions + -// Pure functions + -// Function side effect + -// Unchanging state -// Shared State -// Closures -// Recursion // Partial function application -//First-Class and Pure function -const timeMinut = function (min) { +const multiply = function (x, y) { + return x * y; +} +const multiply2 = multiply.bind(null, 2)// if will be passed argument 'null', the context will not bound with 'this' +// this mean that value null(x = thisArg) & 2(y = arg1) +console.log(multiply2(3)) // 6 +console.log(multiply2(4)) // 8 +console.log(multiply2(5)) // 10 + +//First-Class & Pure function +let timeMinut = function (min) { return min * 60; }; -//Higher-order Functions -function startTimer(timeMinut) { - seconds = timeMinut % 60 // got second - minutes = timeMinut / 60 % 60 // got minute - hour = timeMinut / 60 / 60 % 60 // got hour - // if time gone... - if (timeMinut <= 0) { - clearInterval(timer); - alert('Time is over.'); - } else { - // time output - let strTimer = `${Math.trunc(hour)}:${Math.trunc(minutes)}:${seconds}`; - console.log(strTimer); + +//Higher-order Functions & Shared State (use with timeMinut()) +function greeting(nameInput) { + alert('Hello ' + nameInput + ' you have ' + timeMinut(1) + ' seconds to finish your homework');// timeMinut exist in general scope +} +function userInput(callback) { + let nameInput = prompt('Please enter your name:'); + callback(nameInput); +} +userInput(greeting); //Hello 'your namr' you have 60 seconds to finish your homework + +// Unchanging state +let line = "I am an immutable value"; +let otherline = line.slice(10, 17);// i can to create new state +console.log(otherline); // 'mutable' + +//Closures +function person() { + let name = 'Julia'; + + return function displayName() { + console.log(name); + }; +} +let peter = person(); +peter(); // 'Julia' + +// Recursion +function factorial(x) { + if (x < 0) throw Error("Cannot calculate factorial of a negative number"); + function iter(i, fact) { + if (i === 0) return fact; + else return iter(i - 1, i * fact); + } - --timeMinut; // decrement for timer + return iter(x, 1); } -//timer = setInterval(, 1000); \ No newline at end of file +factorial(5); // 120 \ No newline at end of file From 352bfde82a62004871da458ea5b0088f5c2f3924 Mon Sep 17 00:00:00 2001 From: Lana Trupkina Date: Mon, 15 Mar 2021 05:15:18 +0200 Subject: [PATCH 3/6] HW#5_Trupkina --- HW5(OOP_vs_FP)/HW5_Trupkina.js | 48 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/HW5(OOP_vs_FP)/HW5_Trupkina.js b/HW5(OOP_vs_FP)/HW5_Trupkina.js index a507387..d0aba23 100644 --- a/HW5(OOP_vs_FP)/HW5_Trupkina.js +++ b/HW5(OOP_vs_FP)/HW5_Trupkina.js @@ -207,7 +207,7 @@ student2.getAdvice; //Lana were inspired... mentor.startVacation(); //I'm Dmitry! I'm on vacation! :D -//__________ prototypes__________ +//_____________________ prototypes______________________ function Company2() { //abstract class (constructor) this.name = null; this.vacation = false; @@ -215,34 +215,54 @@ function Company2() { //abstract class (constructor) Company2.prototype.startVacation = function () { }; Company2.prototype.finishVacation = function () { }; -function PersonnelDep2(params) { //Inheritance Class +function PersonnelDep2(name, vacation) { //Inheritance Class from Company2 Company2.call(this); - + this.name = name; + this.vacation = vacation; } -function Students2(params) { //Inheritance Class - Company2.call(this); - this.task = params.name; - this.name = params.t; - this.vacation = params; +PersonnelDep2.prototype.startVacation = function () {//Polymorphism + this.vacation = true; + console.log(`I'm ${this.name}! I'm on vacation! :D`) +} +PersonnelDep2.prototype.finishVacation = function () { + this.vacation = false; + console.log(`${this.name} at work...`); } -Students2.prototype.startVacation = function () { +PersonnelDep2.prototype.getAdvice = function () {//encapsulation(not work) + let getInspirit = `${this.name} were inspired...` + return console.log(getInspirit); +} +function Students2(name, task, vacation) { //Inheritance Class from PersonnelDep2 + PersonnelDep2.call(this, task); + + this.name = name; + this.task = task; + this.vacation = vacation; +} +Students2.prototype.startVacation = function () {//Polymorphism this.vacation = true; - console.log(`I'm on holiday! :D`); + console.log(`I'm ${this.name}! I'm on holiday! :D`); } Students2.prototype.finishVacation = function () { this.vacation = false; - console.log(`holiday is done :(`); + console.log(`holiday is done :( ${this.name} at work...`); } -Students2.prototype.homeworkCheck = function () { +Students2.prototype.homeworkCheck = function (options) { let needTask = 0; this.task = options; - console.log('Need to do ' + (needTask = 14 - options) + ' task'); + console.log('Need to do ' + (needTask = 14 - options) + ' task for ' + this.name); } const student1_2 = new Students2('Dima', 0, false); const student2_2 = new Students2('Lana', 0, false); const mentor2 = new PersonnelDep2('Dmitry', false,); +//___________ + +student1_2.startVacation(); // I'm Dima! I'm on holiday! :D +student2_2.finishVacation(); //holiday is done :( Lana at work... +student2_2.homeworkCheck(5); //Need to do 9 task for Lana +student2_2.getAdvice; //Lana were inspired... (not work) +mentor2.startVacation(); //I'm Dmitry! I'm on vacation! :D -console.log(student1_2);//Students2 {name: "Dima", vacation: "Dima", task: "Dima"} //_________________________________________________ // Functional programming task: // Function side effect From 936474b8a924b334cce15354b7036cb1467acf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B2=D0=B5=D1=82=D0=BB=D0=B0=D0=BD=D0=B0=20=D0=A2?= =?UTF-8?q?=D1=80=D1=83=D0=BF=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Mon, 15 Mar 2021 05:19:13 +0200 Subject: [PATCH 4/6] Update HW5_Trupkina.js --- HW5(OOP_vs_FP)/HW5_Trupkina.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/HW5(OOP_vs_FP)/HW5_Trupkina.js b/HW5(OOP_vs_FP)/HW5_Trupkina.js index d0aba23..49400c5 100644 --- a/HW5(OOP_vs_FP)/HW5_Trupkina.js +++ b/HW5(OOP_vs_FP)/HW5_Trupkina.js @@ -255,7 +255,6 @@ Students2.prototype.homeworkCheck = function (options) { const student1_2 = new Students2('Dima', 0, false); const student2_2 = new Students2('Lana', 0, false); const mentor2 = new PersonnelDep2('Dmitry', false,); -//___________ student1_2.startVacation(); // I'm Dima! I'm on holiday! :D student2_2.finishVacation(); //holiday is done :( Lana at work... @@ -322,4 +321,4 @@ function factorial(x) { } return iter(x, 1); } -factorial(5); // 120 \ No newline at end of file +factorial(5); // 120 From 24f739d2a63954ad67aea19f6b2826bda637037c Mon Sep 17 00:00:00 2001 From: Lana Trupkina Date: Thu, 18 Mar 2021 14:21:02 +0200 Subject: [PATCH 5/6] Additional task * --- HW5(OOP_vs_FP)/HW5_Trupkina.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HW5(OOP_vs_FP)/HW5_Trupkina.js b/HW5(OOP_vs_FP)/HW5_Trupkina.js index 49400c5..b078179 100644 --- a/HW5(OOP_vs_FP)/HW5_Trupkina.js +++ b/HW5(OOP_vs_FP)/HW5_Trupkina.js @@ -211,6 +211,10 @@ mentor.startVacation(); //I'm Dmitry! I'm on vacation! :D function Company2() { //abstract class (constructor) this.name = null; this.vacation = false; + // Additional task* + if (this.constructor === Company2) {//we must not allow creation of instance of abstract class + throw new Error("You cannot create an instance of Abstract Class"); + }; } Company2.prototype.startVacation = function () { }; Company2.prototype.finishVacation = function () { }; @@ -261,6 +265,8 @@ student2_2.finishVacation(); //holiday is done :( Lana at work... student2_2.homeworkCheck(5); //Need to do 9 task for Lana student2_2.getAdvice; //Lana were inspired... (not work) mentor2.startVacation(); //I'm Dmitry! I'm on vacation! :D +let director = new Company2(); // if you try to create instance of abstract class we get: +// Uncaught Error: You cannot create an instance of Abstract Class //_________________________________________________ // Functional programming task: From 8df55d7d1ffeab574342117f04cbd25be1ef85c6 Mon Sep 17 00:00:00 2001 From: Lana Trupkina Date: Thu, 18 Mar 2021 15:24:08 +0200 Subject: [PATCH 6/6] Additional task* --- HW5(OOP_vs_FP)/HW5_Trupkina.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/HW5(OOP_vs_FP)/HW5_Trupkina.js b/HW5(OOP_vs_FP)/HW5_Trupkina.js index b078179..0e123ac 100644 --- a/HW5(OOP_vs_FP)/HW5_Trupkina.js +++ b/HW5(OOP_vs_FP)/HW5_Trupkina.js @@ -232,10 +232,7 @@ PersonnelDep2.prototype.finishVacation = function () { this.vacation = false; console.log(`${this.name} at work...`); } -PersonnelDep2.prototype.getAdvice = function () {//encapsulation(not work) - let getInspirit = `${this.name} were inspired...` - return console.log(getInspirit); -} + function Students2(name, task, vacation) { //Inheritance Class from PersonnelDep2 PersonnelDep2.call(this, task); @@ -263,7 +260,6 @@ const mentor2 = new PersonnelDep2('Dmitry', false,); student1_2.startVacation(); // I'm Dima! I'm on holiday! :D student2_2.finishVacation(); //holiday is done :( Lana at work... student2_2.homeworkCheck(5); //Need to do 9 task for Lana -student2_2.getAdvice; //Lana were inspired... (not work) mentor2.startVacation(); //I'm Dmitry! I'm on vacation! :D let director = new Company2(); // if you try to create instance of abstract class we get: // Uncaught Error: You cannot create an instance of Abstract Class