Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JS Advanced Exam - 25 Jun 2022
- // 01. Car Dealers
- // https://judge.softuni.org/Contests/Practice/Index/3519#0
- function solve() {
- const makeElement = document.getElementById('make');
- const modelElement = document.getElementById('model');
- const fuelElement = document.getElementById('fuel');
- const yearElement = document.getElementById('year');
- const originalCostElement = document.getElementById('original-cost');
- const sellingPriceElement = document.getElementById('selling-price');
- const publishBtn = document.getElementById('publish');
- const tableBodyElement = document.getElementById('table-body');
- const carListElement = document.getElementById('cars-list');
- const profitElement = document.getElementById('profit');
- let CarDetails = {};
- let totalProfit = 0;
- publishBtn.addEventListener('click', publishOffer);
- function publishOffer(e) {
- e.preventDefault();
- CarDetails = {
- make: makeElement.value,
- model: modelElement.value,
- year: yearElement.value,
- fuel: fuelElement.value,
- originalCost: Number(originalCostElement.value),
- sellingPrice: Number(sellingPriceElement.value),
- }
- if (!CarDetails.make ||
- !CarDetails.model ||
- !CarDetails.year ||
- !CarDetails.fuel ||
- !CarDetails.originalCost ||
- !CarDetails.sellingPrice || CarDetails.originalCost >= CarDetails.sellingPrice) {
- alert('Wrong input')
- return;
- }
- let trRow = document.createElement('tr');
- trRow.classList.add('row');
- customTd(CarDetails.make, trRow);
- customTd(CarDetails.model, trRow);
- customTd(CarDetails.year, trRow);
- customTd(CarDetails.fuel, trRow);
- customTd(CarDetails.originalCost, trRow);
- customTd(CarDetails.sellingPrice, trRow);
- let tdBtnContainer = document.createElement('td');
- let editBtn = customBtn('Edit', tdBtnContainer, 'action-btn', 'edit');
- editBtn.addEventListener('click', editHandler)
- let sellBtn = customBtn('Sell', tdBtnContainer, 'action-btn', 'sell');
- sellBtn.addEventListener('click', sellHandler)
- trRow.appendChild(tdBtnContainer);
- tableBodyElement.appendChild(trRow);
- clearInputs();
- }
- function editHandler(e) {
- let offerToEdit = e.target.parentNode.parentNode;
- makeElement.value = offerToEdit.children[0].textContent;
- modelElement.value = offerToEdit.children[1].textContent;
- yearElement.value = offerToEdit.children[2].textContent;
- fuelElement.value = offerToEdit.children[3].textContent;
- originalCostElement.value = offerToEdit.children[4].textContent;
- sellingPriceElement.value = offerToEdit.children[5].textContent;
- offerToEdit.remove();
- }
- function sellHandler(e) {
- let offerToSell = e.target.parentNode.parentNode;
- let make = offerToSell.children[0].textContent;
- let model = offerToSell.children[1].textContent;
- let year = offerToSell.children[2].textContent;
- let originalPrice = offerToSell.children[4].textContent;
- let sellPrice = offerToSell.children[5].textContent;
- let sellDiff = Number(sellPrice) - Number(originalPrice);
- totalProfit += sellDiff;
- let newLi = document.createElement('li');
- newLi.classList.add('each-list');
- customSpan(make, model, newLi);
- customSpan(year, '', newLi);
- customSpan(sellDiff, '', newLi);
- carListElement.appendChild(newLi);
- profitElement.textContent = totalProfit.toFixed(2);
- offerToSell.remove();
- }
- function clearInputs() {
- makeElement.value = '';
- modelElement.value = '';
- yearElement.value = '';
- fuelElement.value = '';
- originalCostElement.value = '';
- sellingPriceElement.value = '';
- }
- function customTd(text, parent) {
- let newTd = document.createElement('td');
- newTd.textContent = text;
- parent.appendChild(newTd);
- return newTd;
- }
- function customBtn(text, parent, class1, class2) {
- let newBtn = document.createElement('button');
- newBtn.textContent = text;
- newBtn.classList.add(class1);
- newBtn.classList.add(class2);
- parent.appendChild(newBtn);
- return newBtn;
- }
- function customSpan(text1, text2, parent) {
- let newSpan = document.createElement('span');
- if (text2) {
- newSpan.textContent = text1 + " " + text2;
- } else {
- newSpan.textContent = text1;
- }
- parent.appendChild(newSpan);
- return newSpan;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement