Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let priceMap = {
- "js-fundamentals": 170,
- "js-advanced": 180,
- "js-applications": 190,
- "js-web": 490,
- };
- let listCourses = document.querySelector("#myCourses > div.courseBody > ul");
- document.querySelector("#availableCourses > div.courseFoot > button").addEventListener("click", () => {
- let type = "";
- let selected = [];
- Array
- .from(document.querySelectorAll("input"))
- .filter(el => el.checked === true)
- .forEach(el =>
- el.value.includes("js") ? selected.push(el.value) : type = el.value
- );
- let sum = getPrice(selected);
- let price = Math.floor(sum);
- let priceForm = document.querySelector("#myCourses > div.courseFoot > p");
- let curPrice = Number(priceForm.innerHTML.split(" ")[1]);
- priceForm.innerHTML = `Cost: ${curPrice + price}.00 BGN`;
- function getPrices(type) {
- if (type === "online") {
- priceMap["js-fundamentals"] = priceMap["js-fundamentals"] * 0.94;
- priceMap["js-advanced"] = priceMap["js-advanced"] * 0.94;
- priceMap["js-applications"] = priceMap["js-applications"] * 0.94;
- priceMap["js-web"] = priceMap["js-web"] * 0.94;
- }
- }
- function transformName(courseName) {
- let tokens = courseName.split("-");
- let fPart = tokens[0].toUpperCase();
- let name = fPart + "-" + String.fromCharCode(tokens[1].charCodeAt(0) - 32);
- for (let i = 1; i < tokens[1].split("").length; i++) {
- name += tokens[1].charAt(i);
- }
- return name;
- }
- function addToCourses(courseName) {
- if (courseName !== "HTML and CSS") {
- courseName = transformName(courseName);
- }
- let applications = document.createElement("li");
- applications.innerHTML = courseName;
- listCourses.appendChild(applications);
- }
- function getPrice(selected) {
- getPrices(type);
- let arrCopy = selected.slice(0, selected.length);
- let price = 0;
- if (selected.indexOf("js-fundamentals") !== -1 && selected.indexOf("js-advanced") !== -1) {
- price += priceMap[selected.shift()] + (priceMap[selected.shift()] * 0.90);
- if (selected.indexOf("js-applications") !== -1) {
- price += priceMap[selected.shift()];
- price *= 0.94;
- }
- if (selected.length === 1) {
- price += priceMap[selected.shift()];
- arrCopy.push('HTML and CSS');
- }
- }
- while (selected.length > 0) {
- price += priceMap[selected.shift()];
- }
- arrCopy.forEach(el => addToCourses(el));
- return price;
- }
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement