JS Advanced - Exam 24.10.20
Здравйте,
Моля някой, който е изкарал пълен брой точки на изпита по JS Advanced от 24.10.2020 да качи решения на задачите. Благодаря предварително!
Здравйте,
Моля някой, който е изкарал пълен брой точки на изпита по JS Advanced от 24.10.2020 да качи решения на задачите. Благодаря предварително!
Здравей, давам ти решенията на 2 и 3 задача. На първа нямам пълен брой точки.
Задача 2.
function solveClasses() {
class Developer {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.baseSalary = 1000;
this.tasks = [];
this.experience = 0;
}
addTask(id, taskName, priority) {
if (priority === 'high') {
this.tasks.unshift({ id, taskName, priority });
}
else {
this.tasks.push({ id, taskName, priority });
}
return `Task id ${id}, with ${priority} priority, has been added.`;
}
doTask() {
let task;
let index = 0;
for (let i = this.tasks.length; i > 0; i++) {
if (this.tasks[i].priority === 'high') {
task = this.tasks[i];
break;
}
}
index = this.tasks.indexOf(task);
this.tasks = this.tasks.splice(index, 1);
return `${task.taskName}, you have finished all your tasks. You can rest now.`;
}
getSalary() {
return `${this.firstName} ${this.lastName} has a salary of: ${this.baseSalary}`;
}
reviewTasks() {
let result = [];
result.push('Tasks, that need to be completed:');
for (let task of this.tasks) {
result.push(`${task.id}: ${task.taskName} - ${task.priority}`);
}
return result.join('\n');
}
}
class Junior extends Developer {
constructor(firstName, lastName, bonus, experience) {
super(firstName, lastName);
this.baseSalary = this.baseSalary + bonus;
this.tasks = [];
this.experience = experience;
}
learn(years) {
this.experience += Number(years);
}
}
class Senior extends Developer {
constructor(firstName, lastName, bonus, experience) {
super(firstName, lastName);
this.baseSalary = this.baseSalary + bonus;
this.experience = Number(experience) + 5;
}
changeTaskPriority(taskId) {
let task = this.tasks.find(x => x.id === taskId);
let index = this.tasks.indexOf(task);
this.tasks = this.tasks.splice(index, 1);
if (task.priority === 'high') {
task.priority = 'low';
this.tasks.push(task);
}
else {
task.priority = 'high';
this.tasks.unshift(task);
}
return task;
}
}
return {
Developer,
Junior,
Senior
}
}
Задача 3.
class Parking {
constructor(capacity) {
this.capacity = Number(capacity);
this.vehicles = [];
}
addCar(carModel, carNumber) {
if (this.capacity === this.vehicles.length) {
throw new Error('Not enough parking space.');
}
this.vehicles.push({ carModel, carNumber, payed: false });
return `The ${carModel}, with a registration number ${carNumber}, parked.`;
}
removeCar(carNumber) {
let car = this.vehicles.find(x => x.carNumber === carNumber);
if (!car) {
throw new Error('The car, you\'re looking for, is not found.');
}
if (car.payed === false) {
throw new Error(`${carNumber} needs to pay before leaving the parking lot.`);
}
let index = this.vehicles.indexOf(car);
this.vehicles = this.vehicles.splice(index, 1);
return `${carNumber} left the parking lot.`;
}
pay(carNumber) {
let car = this.vehicles.find(x => x.carNumber === carNumber);
if (!car) {
throw new Error(`${carNumber} is not in the parking lot.`);
}
if (car.payed === true) {
throw new Error(`${carNumber}'s driver has already payed his ticket.`);
}
car.payed = true;
return `${carNumber}'s driver successfully payed for his stay.`;
}
getStatistics(carNumber) {
if (carNumber !== undefined) {
let car = this.vehicles.find(x => x.carNumber === carNumber);
return `${car.carModel} == ${car.carNumber} - ${car.payed === true ? 'Has payed' : 'Not payed'}`;
}
let result = [];
let count = 0;
for (let i = 0; i < this.vehicles.length; i++) {
count++;
}
result.push(`The Parking Lot has ${this.capacity - count} empty spots left.`);
let sorted = this.vehicles.sort((a, b) => a.carModel.localeCompare(b.carModel));
for (let car of sorted) {
result.push(`${car.carModel} == ${car.carNumber} - ${car.payed === true ? 'Has payed' : 'Not payed'}`);
}
return result.join('\n');
}
}
Employees - https://pastebin.com/UhN4j7yf
Parking - https://pastebin.com/TEyaLmbN
Нямам пълни точки на 1ва...
За 1ва по-скоро се интересувам, дали някой е изпълнил условието за сортиране по дата.
мисля, че успях. за мое съжаление, след като ми свърши времето. не знам колко точки би дало в система.
поглдедни го, ако искаш. виж как работи в браузъра.
-------------------------------------------------------------------------------------------------------------------
function solve() {
let addBtn = document.querySelector('.form-control button');
let lectureNameEl = document.querySelector('input[name="lecture-name"]');
let dateEl = document.querySelector('input[name="lecture-date"]');
let selectEl = document.querySelector('.form-control > select');
let training = document.querySelector('.modules');
addBtn.addEventListener('click', function (e) {
e.preventDefault();
let modules = [...document.querySelectorAll('.modules h3')];
if (lectureNameEl.value === '' || dateEl.value === '' || selectEl.value === 'Select module') { return; }
let lectureName = lectureNameEl.value;
let date = dateEl.value;
let currentModuleName = selectEl.value;
date = date.replace('-', '/');
date = date.replace('-', '/');
date = date.replace('T', ' - ');
let module = modules.find(mod => mod.textContent.includes(currentModuleName.toUpperCase()));
if (module === undefined) {
module = el('div', [
el('h3', `${currentModuleName.toUpperCase()}-MODULE`)
], { className: 'module' }) // end div;
let delBtn = el('button', 'Del', { className: 'red' });
delBtn.addEventListener('click', deleteElement);
let ul = el('ul');
let li = el('li', [
el('h4', `${lectureName} - ${date}`),
delBtn
], { className: 'flex' });
ul.appendChild(li);
module.appendChild(ul);
training.appendChild(module);
} else {
let div = module.parentElement;
let delBtn = el('button', 'Del', { className: 'red' });
delBtn.addEventListener('click', deleteElement);
let ul = div.querySelector('ul');
let li = el('li', [
el('h4', `${ lectureName } - ${ date }`),
delBtn
], { className: 'flex' });
ul.appendChild(li);
div.appendChild(ul);
}
[...document.querySelectorAll('.modules ul')]
.forEach(u => {
let li = [...u.querySelectorAll('li')].sort((a, b) => {
let startIndexA = a.textContent.indexOf(' ');
let startIndexB = b.textContent.indexOf(' ');
let tempA = a.textContent.substr(startIndexA);
let tempB = b.textContent.substr(startIndexB);
return tempA.localeCompare(tempB); })
.forEach(l => u.appendChild(l));
})
function deleteElement(e) {
let li = e.target.parentElement;
let ul = li.parentElement;
li.remove();
let ulArr = [...ul.querySelectorAll('li')];
if (ulArr.length === 0) { ul.parentElement.remove() }
} // end deleteElement;
}) // end addBtn;
function el(type, node, attributes) {
let element = document.createElement(type);
if (node == undefined) { return element; }
if (attributes !== undefined) {
Object.assign(element, attributes);
}
if (Array.isArray(node)) {
node.forEach(e => appendEl(e));
} else {
appendEl(node);
}
function appendEl(currentNode) {
if (typeof currentNode == 'string' || typeof currentNode == 'number') {
element.innerText = currentNode;
return;
}
element.appendChild(currentNode);
} // end appendEl;
return element;
} // end el;
};