Skip to content

Commit

Permalink
Merge pull request #1 from MastersAcademy/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
VolodymyrSkarb authored Dec 13, 2018
2 parents 0d6e94a + 05915bf commit ec4a0cd
Show file tree
Hide file tree
Showing 152 changed files with 10,050 additions and 1,058 deletions.
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);
75 changes: 31 additions & 44 deletions homeworks/Dmytro.Tyshchenko_DmytroTy/homework_6/task2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const horsesList = [['Big Racket', 'Thoroughbred'], ['Shareef Dancer', 'Thoroughbred'],
['Green Monkey', 'Thoroughbred'], ['Annihilator', 'Thoroughbred'],
['Seattle Dancer', 'Thoroughbred'], ['Mystic Park', 'Standardbred'],
['Pine Chip', 'Standardbred'], ['Sardar\t', 'Thoroughbred'],
['Poetin\t', 'Brandenburger'], ['Lord Sinclair', 'Bavarian Warmblood']];
const horsesList = [{ name: 'Big Racket', breed: 'Thoroughbred' }, { name: 'Shareef Dancer', breed: 'Thoroughbred' },
{ name: 'Green Monkey', breed: 'Thoroughbred' }, { name: 'Annihilator', breed: 'Thoroughbred' },
{ name: 'Seattle Dancer', breed: 'Thoroughbred' }, { name: 'Mystic Park', breed: 'Standardbred' },
{ name: 'Pine Chip', breed: 'Standardbred' }, { name: 'Sardar\t', breed: 'Thoroughbred' },
{ name: 'Poetin\t', breed: 'Brandenburger' }, { name: 'Lord Sinclair', breed: 'Bavarian Warmblood' }];

class Horse {
constructor(name, breed) {
Expand All @@ -15,70 +15,57 @@ class Racer extends Horse {
constructor(name, breed) {
super(name, breed);
this.distance = 0;
this.speed = 0;
this.setSpeed();
this.time = 0;
}

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

run(timeout) {
const that = this;
const intervalID = setInterval(() => {
this.distance += this.speed;
this.time++;
this.setSpeed();

let intervalID;

function racing() {
that.distance += that.speed;
that.time++;
that.setSpeed();
if (that.time >= timeout) clearInterval(intervalID);
}

intervalID = setInterval(racing, 1000);
if (this.time >= timeout) clearInterval(intervalID);
}, 1000);
}
}

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

createRace() {
this.horses = horsesList.map(arr => new Racer(arr[0], arr[1]));
this.horses = horsesList.map(obj => new Racer(obj.name, obj.breed));
}

startRece(timeout) {
startRace(timeout) {
this.time = 0;
this.finish = timeout;

this.horses.forEach(obj => obj.run(timeout));

const that = this;
setTimeout(() => {
this.intervalID = setInterval(() => this.logRacing(), 2000);
}, 100);
}

function racing(time) {
function createLogRacing(str, obj) {
return `${str}${obj.name}\t(${obj.breed})\t\trun\t${obj.distance} m\n`;
}
logRacing() {
this.time += 2;

function logRacing() {
console.log(that.horses.reduce(createLogRacing, `Time = ${time} c:\n`));
if (time < timeout) racing(time + 2);
else {
that.horses.sort((a, b) => b.distance - a.distance);
console.log(`Winner: ${that.horses[0].name} congratulate the champion!`);
}
}
function createLogRacing(str, obj) {
return `${str}${obj.name}\t(${obj.breed})\t\trun\t${obj.distance} m\n`;
}

function synchronization() {
if (that.horses.some(obj => obj.time < time)) setTimeout(synchronization, 16);
else logRacing();
}
console.log(this.horses.reduce(createLogRacing, `Time = ${this.time} c:\n`));

setTimeout(synchronization, 2000);
if (this.time >= this.finish) {
clearInterval(this.intervalID);
this.horses.sort((a, b) => b.distance - a.distance);
console.log(`Winner: ${this.horses[0].name} congratulate the champion!`);
}

racing(2);
}
}

const race1 = new Race();

race1.createRace();
race1.startRece(10);
race1.startRace(10);
34 changes: 34 additions & 0 deletions homeworks/Makc.Sas_MakcSas/homework_6/homework_6_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const users = {};
const phone = {};
function userObjects(user) {
let fullName;
let phoneNumber;
Object.defineProperties(users, {
fullName: {
set(name) {
fullName = name.toString().toLowerCase().replace(/\b\w/g, value => value.toUpperCase());
},
get() {
return fullName;
},
},
});
Object.defineProperties(phone, {
phoneNumber: {
set(numPhone) {
phoneNumber = '+'.concat(numPhone.toString().replace(/\D/g, ''));
},
get() {
return phoneNumber;
},
},
});
return user;
}
const user = userObjects({});
user.fullName = 'aNna-mAria joHNs';
console.log(user.fullName);
user.phone = '+38(096)-111-22-33';
console.log(user.phone);
user.phone = '38(096)-111-22-33';
console.log(user.phone);
67 changes: 67 additions & 0 deletions homeworks/Makc.Sas_MakcSas/homework_6/homework_6_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 normalize = [
{ name: 'Buck', breed: 'Zebra' },
{ name: 'Widowmaker', breed: 'Pony' },
{ name: 'Cyril Proudbottom', breed: 'Arabian' },
{ name: 'Snowball', breed: 'Przewalski' },
{ name: 'Ahill', breed: 'Zebra' },
{ name: 'Tagged', breed: 'Arabian' },
{ name: 'Captain', breed: 'Przewalski' },
{ name: 'Han', breed: 'Arabian' },
{ name: 'Major', breed: 'Arabian' },
{ name: 'Max', breed: 'Pony' },
];
normalize.forEach((value) => {
this.horses.push(new Racer(value.name, value.breed));
});
}

startRace() {
this.horses.forEach(value => value.run());
for (let i = 0; i < 5; i++) {
setTimeout(() => {
this.horses.forEach(value => console.log(value));
}, 2000 * i);
}
setTimeout(() => {
this.horses.sort((value1, value2) => value2.distance - value1.distance);
console.log(`Winner: ${this.horses[0].name}`);
}, 10000);
}
}
const horseRacing = new Race();
horseRacing.createRace();
horseRacing.startRace();
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const user = {};
Object.defineProperties(user, {
fullName: {
get() {
return this.userFullName;
},
set(value) {
const re = /\b\w/g;
this.userFullName = value.trim().toLowerCase().replace(re, str => str.toUpperCase());
},
},
phone: {
get() {
return this.userPhone;
},
set(value) {
const re = /^\+[\d]*|\d/g;
this.userPhone = value.trim().match(re).join('');
},
},
});

user.fullName = ' aLi-Ibn-haTab kiziLbek jr.';
console.log(user.fullName);
user.fullName = 'aNna-mAria joHNs';
console.log(user.fullName);
user.phone = ' +32df113256';
console.log(user.phone);
user.phone = '+38(096)-111-22-33';
console.log(user.phone);
user.phone = '38(096)+111b22+33';
console.log(user.phone);
Loading

0 comments on commit ec4a0cd

Please sign in to comment.