Skip to content

Commit

Permalink
Merge pull request #1 from MastersAcademy/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
DmytroTiulpa authored Dec 16, 2018
2 parents 5226510 + 5240388 commit d469e6d
Show file tree
Hide file tree
Showing 979 changed files with 45,992 additions and 1,095 deletions.
78 changes: 78 additions & 0 deletions homeworks/Aleksandr.Turuta_turuta7/homework_5/home_5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// the task 1
function createCounter() {
let num = 0;
return ((ppp) => {
num += ppp;
return num;
});
}


function sumArray(array) {
const fn = createCounter();
return array.map(fn);
}


console.log(sumArray([10, -10, 10, -10, 10]));
console.log(sumArray([0, 0, 0, 0, 0]));
console.log(sumArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

// the task 2
const employee = ['header', 'menu', 'menu_item', 'tabs',
'tab_item', 'menu', 'link', 'tabs', 'tab_item', 'menu',
'menu_item', 'menu', 'menu_item'];

function arr(ob) {
const res = {};
ob.forEach((x) => {
if (x in res) {
res[x] += 1;
} else {
res[x] = 1;
}
});
return res;
}
console.log(arr(employee));

// the task 3
const Array = { b: 'c', d: { e: 'f' } };

function newArray(arr1) {
const newObject = {};
Object.keys(arr1).forEach((x) => {
if (typeof (arr1[x]) === 'object') {
newObject[x] = newArray(arr1[x]);
} else {
newObject[x] = arr1[x];
}
});

return newObject;
}
const copy = newArray(Array);
console.log(copy.b);

// the task 4
const people = [
{ id: 1, name: 'Nick', friends: [2, 5, 6] },
{ id: 2, name: 'John', friends: [1, 3] },
{ id: 3, name: 'Mike', friends: [2, 5] },
{ id: 4, name: 'Janny', friends: null },
{ id: 5, name: 'Edward', friends: [1, 3] },
{ id: 6, name: 'Jeen', friends: [5, 1] },
];

function getPeople(nameid) {
const user = people.find(x => x.id === nameid);
if (typeof user === 'undefined') {
return null;
}
if (Array.isArray(user.friends)) {
return people.filter(person => user.friends.includes(person.id));
}
return [];
}
console.log(getPeople(2));
console.log(getPeople(4));
100 changes: 100 additions & 0 deletions homeworks/Aleksandr.Turuta_turuta7/homework_6/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// the task 1
const user = {};
Object.defineProperties(user, {
fullName: {
enumerable: true,
configurable: true,
writeble: true,
set: (name) => {
this.fullName = name
.replace(/([[A-Za-z])\S/g, s => s.toLowerCase())
.replace(/(^|\s|[-])\S/g, s => s.toUpperCase());
},
get: () => this.fullName,
},
phone: {
enumerable: true,
configurable: true,
writeble: true,
set: (num) => {
this.phone = num
.replace(/(?![/+])\D+/g, '');
},
get: () => this.phone,
},
});
user.fullName = 'aNna-mAria joHNs';
console.log(user.fullName);
user.phone = '+38(068)77-22-9-44';
console.log(user.phone);

// the task 2
class Horse {
constructor(name, breed) {
this.name = name;
this.breed = breed;
}
}
class Racer extends Horse {
constructor(name, breed) {
super(name, breed);
this.distance = 0;
this.speed = 0;
this.setSpeed();
}

setSpeed() {
this.speed = Math.floor(Math.random() * (15 - 10) + 10);
}

run() {
for (let i = 0; i < 10; i++) {
setTimeout(() => {
this.distance += this.speed;
this.setSpeed();
console.log(this);
}, 1000 * i);
}
}
}
class Race {
constructor() {
this.horses = [];
}

createRace() {
const stabling = [
{ name: 'Apollo', breed: 'horses1' },
{ name: 'Augustus', breed: 'horses2' },
{ name: 'Aklon', breed: 'horses3' },
{ name: 'Antey', breed: 'horses4' },
{ name: 'Ahill', breed: 'horses5' },
{ name: 'Angel', breed: 'horses6' },
{ name: 'Aston', breed: 'horses7' },
{ name: 'Adonis', breed: 'horses8' },
{ name: 'Bella', breed: 'horses9' },
{ name: 'Bonita', breed: 'horses10' },
];
stabling.forEach((element) => {
this.horses.push(new Racer(element.name, element.breed));
});
}

startRace() {
this.horses.forEach(a => a.run());
for (let i = 0; i < 5; i++) {
setTimeout(() => {
this.horses.forEach((element) => {
console.log(element);
});
}, 2000 * i);
}
setTimeout(() => {
this.horses.sort((a, b) => a.distance - b.distance);
console.log(`Win horse: ${this.horses[0].name}`);
}, 10000);
}
}
const horseRacing = new Race();
horseRacing.createRace();
horseRacing.startRace();
15 changes: 15 additions & 0 deletions homeworks/Alexey.Berkut_AlBerkut/homework_5/arraySum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const array1 = [10, -10, 10, -10, 10];
const array2 = [0, 0, 0, 0, 0];
const array3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function array(input) {
let newArray = 0;
return input.map((num) => {
newArray += num;
return newArray;
});
}

console.log(array(array1));
console.log(array(array2));
console.log(array(array3));
26 changes: 26 additions & 0 deletions homeworks/Alexey.Berkut_AlBerkut/homework_5/getPeople.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const people = [
{ id: 1, name: 'Nick', friends: [2, 5, 6] },
{ id: 2, name: 'John', friends: [1, 3] },
{ id: 3, name: 'Mike', friends: [2, 5] },
{ id: 4, name: 'Janny', friends: null },
{ id: 5, name: 'Edward', friends: [1, 3] },
{ id: 6, name: 'Jeen', friends: [5, 1] },
];


function getPeople(userId) {
const person = people.find(user => user.id === userId); // get user;
if (typeof person === 'undefined') {
return 'User not found';
}
if (Array.isArray(person.friends)) {
const friends = people.filter(friend => person.friends.includes(friend.id)); // get friends;
console.log(person.name);
return friends;
}
return `${person.name} friends not found`;
}

console.log(getPeople(2));
console.log(getPeople(4));
console.log(getPeople(100500));
23 changes: 23 additions & 0 deletions homeworks/Alexey.Berkut_AlBerkut/homework_5/innerCopy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Small copy. Nested object passed reference;
function innerCopy(input) {
const objCopy = Object.assign({}, input);
return objCopy;
}
console.log(innerCopy(obj)); */

function interCopy(input) {
const result = {}; // new empty object;
Object.keys(input).forEach((key) => {
if (typeof (input[key]) === 'object') {
interCopy(input[key]);
}
result[key] = input[key];
});
return result;
}

const obj = { b: 'c', d: { e: 'f' } };
const objCopy = interCopy(obj);

console.log(objCopy);
17 changes: 17 additions & 0 deletions homeworks/Alexey.Berkut_AlBerkut/homework_5/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const classes = ['header', 'menu', 'menu_item', 'tabs',
'tab_item', 'menu', 'link', 'tabs', 'tab_item', 'menu',
'menu_item', 'menu', 'menu_item'];

function obj(input) {
const newobject = {};
input.forEach((item) => {
if (item in newobject) {
newobject[item] += 1;
} else {
newobject[item] = 1;
}
});
return newobject;
}

console.log(obj(classes));
83 changes: 83 additions & 0 deletions homeworks/Alexey.Berkut_AlBerkut/homework_6/horse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class Horse {
constructor(name, breed) {
this.name = name;
this.breed = breed;
}
}

class Racer extends Horse {
constructor(name, breed) {
super(name, breed);
this.distance = 0;
this.speed = 0;
}

setSpeed() {
this.speed = Math.floor(Math.random() * 5) + 10;
}

run() {
this.timer = setInterval(() => {
this.distance += this.speed;
this.setSpeed();
}, 1000);
}

stop() {
if (this.timer) clearInterval(this.timer);
}
}

class Race {
constructor() {
this.horses = [];
}

createRace(horses) {
this.horses = horses;
}

startRace() {
if (this.horses.length) {
this.horses.forEach(horse => horse.run());
this.logRacerState();
setTimeout(this.stopRace.bind(this), 10000);
}
}

logRacerState() {
this.timer = setInterval(() => {
this.horses.forEach((horse) => {
console.log(`${horse.breed} ${horse.name} : ${horse.distance}`);
});
}, 2000);
}

stopRace() {
if (this.timer) clearInterval(this.timer);
this.horses.forEach(horse => horse.stop());
console.log(this.findWinner().breed, this.findWinner().name, 'Win!');
}

findWinner() {
this.horses.sort((i1, i2) => i1.distance - i2.distance);
return this.horses[0];
}
}

const HORSES = [
new Racer('Roach', 'Geralt\'s Horse'),
new Racer('Pegasus', 'Dandelion\'s white gelding'),
new Racer('Aard', 'Windhorse'),
new Racer('Ignis', 'Firehorse'),
new Racer('Quen', 'Battlehorse'),
new Racer('Axii', 'Calmhorse'),
new Racer('Yrden', 'Magichorse'),
new Racer('Kelpie', 'Ciri\'s Horse'),
new Racer('Scorpion', 'Eskel\'s horse'),
new Racer('Lexa', 'Student'),
];

const race = new Race();
race.createRace(HORSES);
race.startRace();
33 changes: 33 additions & 0 deletions homeworks/Alexey.Berkut_AlBerkut/homework_6/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const NAME_FILTER = /\b\w/g;
const PHONE_FILTER = /(?![/+])\D+/g;

function User() {
let fullName;
let phone;
Object.defineProperties(this, {
fullName: {
set(name) {
fullName = name.toLowerCase().replace(NAME_FILTER, l => l.toUpperCase());
},
get() {
return fullName;
},
},
phone: {
set(phoneNum) {
phone = phoneNum.replace(PHONE_FILTER, '');
},
get() {
return phone;
},
},
});
}

const user = new User();
user.fullName = 'aNna-mAria joHNs';
console.log(user.fullName);
user.phone = '+38(096)-111-22-33';
console.log(user.phone);
user.phone = '38(096)-111b-22-33';
console.log(user.phone);
Loading

0 comments on commit d469e6d

Please sign in to comment.