diff --git a/.gitignore b/.gitignore index b3676cf..9adfa59 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,9 @@ +<<<<<<< HEAD +/**/node_modules +.idea +/1 +/2 +======= /**/node_modules/ .idea/ +>>>>>>> b2e4fe7196a9b58dee66f89fffd5fbb517995952 diff --git a/.vscode/launch.json b/.vscode/launch.json index 754e8bf..7ccef61 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,12 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "node", + "request": "launch", + "name": "HOMEWORK 3 Unit app", + "program": "${workspaceFolder}/3/App2_hw.js" + }, { "type": "node", "request": "launch", @@ -16,17 +22,24 @@ "name": "Launch 2 Unit app", "program": "${workspaceFolder}/2/top10.js" }, + { + "type": "node", + "request": "launch", + "name": "L. Obj 2 Unit app", + "program": "${workspaceFolder}/2/obj.js" + }, { "type": "node", "request": "launch", "name": "Launch 3 Unit app", - "program": "${workspaceFolder}/3/app2.js" + "program": "${workspaceFolder}/3/app2_hw.js" }, { "type": "node", "request": "launch", "name": "Launch Pilot", "program": "${workspaceFolder}/pilot/app.js" + }, { "type": "node", @@ -45,6 +58,12 @@ "request": "launch", "name": "CallBack app ( Anton)", "program": "${workspaceFolder}/4/callback.js" + }, + { + "type": "node", + "request": "launch", + "name": "Promises app ( Evgen)", + "program": "${workspaceFolder}/4/Promises_examples.js" } ] } diff --git a/1/.eslintrc.js b/1/.eslintrc.js index 88167f2..a48d372 100644 --- a/1/.eslintrc.js +++ b/1/.eslintrc.js @@ -14,7 +14,7 @@ module.exports = { ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", diff --git a/2/.eslintrc.js b/2/.eslintrc.js index 156976f..71fd531 100644 --- a/2/.eslintrc.js +++ b/2/.eslintrc.js @@ -11,7 +11,7 @@ module.exports = { ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", diff --git a/2/app.js b/2/app.js index d88d688..1cc08b6 100644 --- a/2/app.js +++ b/2/app.js @@ -7,19 +7,116 @@ * "}{}{" invalid, * "{{}{}{}}" valid; */ +// var str = '{}}{{}', +// strLength = str.length, +// lastChar = str.charAt(strLength-1), +// firstChar = str.charAt(0); +// console.log('First cond: EVEN number of chars: ' + strLength); +// console.log('Second cond: Must end with "}" : ' + lastChar); +// console.log('Thrid cond: Must start from "{" : ' + firstChar); +// // if (str.indexOf('}') > 0 && firstChar == '{') { -function validation(x){ - // последовательно просмотреть все символы один за другим - // 1, 2, 3, 4, .... - // - // если у нас длина строки нечетная, тогда сразу invalid - // введем счетчик и если { тогда мы его увеличиваем, если } тогда уменьшаем +// // } - return true; +// if (strLength % 2 == 0 && lastChar == '}' && firstChar == '{' && (str.indexOf('}') > 0 && firstChar == '{')) { +// console.log('THIS STRING IS VALID'); +// } else { +// console.log('NEIN!! STRING IS INVALID'); +// } + +const str = '{{}}}{{}'; +const valSt = 'Valid string'; +const invalSt = 'Invalid string'; +var bracketCount = 0; + +function StrVal(str){ + + for (let i = 0; i < str.length; i++ ) { + if (str[i] == '{' ) { + bracketCount++; + } + else if (str[i] == '}') { + bracketCount-- ; + if (bracketCount < 0) { + return invalSt; + } + } + } + + if (bracketCount == 0) { + return valSt; + } + return invalSt; + +} +// if (str === false ){ +// console.log('Invalid string Str'); +// } else{ +// console.log('Valid Str'); +// } +console.log(StrVal(str)); + +// function validation(x){ +// // последовательно просмотреть все символы один за другим +// // 1, 2, 3, 4, .... +// // +// // если у нас длина строки нечетная, тогда сразу invalid +// // введем счетчик и если { тогда мы его увеличиваем, если } тогда уменьшаем + +// return true; +// } + +// const validatedData = '{{}'; + +// const isValid = validation(validatedData); + +// console.log(`"${validatedData}" is Valid ? "${isValid}"`); +//---------------------------Task4---------------------------- +//array 0..100 +// var timeBefore = Date.now(); +// var randomArr = []; + +// for (var i = 0 ; i <= 100; i++) { +// randomArr.push(+i); +// } +// var timeAfrer = Date.now(); +// var timeDiff = timeBefore - timeAfrer; +// console.log('finding top10 took' + timeDiff + 'ms'); + +// var timeBefore = Date.now(); +const timeBefore = new Date().getTime() / 1000; +function RandomNum(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +function CreateRandomArr(len) { + // var len = len || 20; + for (var i = 0, arr = []; i < len; i++) { + arr.push(RandomNum(0, 1000)); + } + return arr; } +var arr = CreateRandomArr(1000000); +const n = arr.length; //getting array.length in n + +console.log('Number of elements in the array is: ' + n); -const validatedData = '{{}'; +function CompareNum(a, b) { + return a - b; +} +//find the top10 from arr +function FindTop10() { + let sortedArray = arr.sort(CompareNum); + let top10 = []; + for (let i = sortedArray.length - 1; i >= (sortedArray.length-10); i--) { + top10.push(sortedArray[i]); + } + return top10; +} -const isValid = validation(validatedData); +var top10Arr = FindTop10(); +console.log('HERE WE GO! TOP 10: ' + top10Arr); -console.log(`"${validatedData}" is Valid ? "${isValid}"`); +var timeAfrer = new Date().getTime() / 1000; +var timeDiff = timeBefore - timeAfrer; +console.log('Time spent on executing the script' + timeDiff + ' sec'); \ No newline at end of file diff --git a/3/.eslintrc.js b/3/.eslintrc.js index 156976f..71fd531 100644 --- a/3/.eslintrc.js +++ b/3/.eslintrc.js @@ -11,7 +11,7 @@ module.exports = { ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", diff --git a/3/App2_hw.js b/3/App2_hw.js new file mode 100644 index 0000000..7d573f6 --- /dev/null +++ b/3/App2_hw.js @@ -0,0 +1,57 @@ +class ArrayMaker { + constructor(size, min = 0 , max = 1000) { + this.size = size; + this.min = min; + this.max = max; + } + _numRandomizer(){ + return Math.round(Math.random()*this.max + this.min); + } + createArray(){ + const array = []; + for (let i = 0; i < this.size; i++ ){ + array.push(this._numRandomizer); + } + return array; + + } + +} + +class Timer { + constructor(isSec){ + this.isSec = isSec; + console.log(`Created as "${this.isSec ? 'sec' : 'ms'}" timer`); + } + start(){ + const now = new Date(); + this.timer = now.getTime(); + console.log(`Started at ${now.getHours()}: ${now.getMinutes()}`); + } + finish() { + const now = new Date(); + const timeDiff = now.getTime() - this.timer; + console.log(`finished at ${ this.isSec ? timeDiff / 1000 : timeDiff } sec`); + } + + +} + +const timerSec = new Timer(true); +const timerMs = new Timer(true); +const arrayMaker = new ArrayMaker (1000000); +// console.log(arrayMaker); +const array1 = arrayMaker.createArray(); +// console.log(array1); + +try { + timerSec.start(); + timerMs.start(); + + timerSec.finish(); + timerMs.finish(); + + console.log(`Array ${array1.length} was created`); +} catch(err){ + console.error(err); +} \ No newline at end of file diff --git a/3/app.js b/3/app.js index 121420c..686852c 100644 --- a/3/app.js +++ b/3/app.js @@ -3,9 +3,9 @@ This array must be generated from random 0...1000 and print duration (sec) */ -const min = 5; +const min = 0; const max = 1000; -const size = 10000000; +const size = 1000000; class Timer { constructor(isSeconds){ @@ -37,19 +37,37 @@ function generateRandomArray(size, min, max){ function randomElement(min, max){ return Math.round(Math.random() * max * 100 + min) / 100; } + + + +} +// 'use strict' +//HOW DO I DO THIS +function compareAb(a, b){ + return a - b; +} +let Filtre = class Compare { + Co +}(a, b) { a - b;} + +function getTop10() { + const sortedArr = randomArray.sort(compareAb); + const top10 = []; + for (let i = sortedArr.length - 1; i>= (sortedArr.length-10); i--) { + top10.push(sortedArr[i]); + } + return top10; } try { timerSeconds.start(); timerMilliseconds.start(); const data = generateRandomArray(size, min, max); + const top10Here = getTop10(); timerSeconds.finish(); timerMilliseconds.finish(); console.log(`Array ${data.length} was created`); } catch(err){ console.error(err); -} - - - +} \ No newline at end of file diff --git a/4/.eslintrc.js b/4/.eslintrc.js index 156976f..71fd531 100644 --- a/4/.eslintrc.js +++ b/4/.eslintrc.js @@ -11,7 +11,7 @@ module.exports = { ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", diff --git a/4/Promises_examples.js b/4/Promises_examples.js new file mode 100644 index 0000000..aafef82 --- /dev/null +++ b/4/Promises_examples.js @@ -0,0 +1,48 @@ +function applyForJob(docs) { + console.log('Applying for a job...'); + let promise = new Promise(function(resolve, reject){ + setTimeout(function(){ + Math.random() > 0.5 ? resolve({}) : reject('Apply for the job has been rejected'); + }, 1500); + }); + return promise; +} + + +function approveApply(permission){ + console.log('WE GOT APPROVED'); + return permission; +} +// approved => { +// console.info('Ur apply has been approved'); +// return approved; +// }; + +function celebrate(permission){ + console.log(permission); + console.log('------------------------'); + console.log('EAT=>SLEEP=>RAVE=>REPEAT'); + return new Promise(function(resolve, reject){ + setTimeout(() => resolve(permission), 1500); + }); +} + +function getReady(){ + console.log('------------------------'); + console.log('Get pants and shirt steamed etc.'); + return console.log('We are ready to work,' + {}); +} + +applyForJob({}) + .then(approveApply) + .then(celebrate) + .then(getReady) + .catch(error => console.error(error)); + + +// function(approved){ +// console.info('Ur apply is approved!'); +// }, +// function(reason){ +// console.error(reason); +// }); \ No newline at end of file diff --git a/pilot/app.js b/pilot/app.js index 3d02398..5578d81 100644 --- a/pilot/app.js +++ b/pilot/app.js @@ -24,12 +24,21 @@ class Generator{ this.accuracy = decimalPlaces * 10; } generateData(size){ - const hugeData = []; + const hugeData = []; for(let i=0; i { + // const a = Math.random() * this.max + this.min; + // const b = Math.round(a * this.accuracy); + // return b; + // } + + function _random() { const a = Math.random() * this.max + this.min; const b = Math.round(a * this.accuracy) / this.accuracy