Todorov_Stanimir

01. SoftUni Courses

Feb 9th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    let prices = {
  3.       'JS-Fundamentals': 170,
  4.       'JS-Advanced': 180,
  5.       'JS-Applications': 190,
  6.       'JS-Web': 490,
  7.    }
  8.    let names = {
  9.       'js-fundamentals': 'JS-Fundamentals',
  10.       'js-advanced': 'JS-Advanced',
  11.       'js-applications': 'JS-Applications',
  12.       'js-web': 'JS-Web',
  13.       'HTML and CSS': 'HTML and CSS',
  14.    }
  15.  
  16.    document.querySelector('button').addEventListener('click', (el) => {
  17.       let choisedCoursies = [...document.querySelectorAll("[type='checkbox']")]
  18.          .filter(el => el.checked === true)
  19.          .map(el => names[el.value]);
  20.  
  21.       let price = choisedCoursies.reduce((totPrice, course) => totPrice + prices[course], 0)
  22.  
  23.       if (choisedCoursies.includes("JS-Advanced") && choisedCoursies.includes("JS-Fundamentals")) {
  24.          prices['JS-Advanced'] = 0.90 * prices['JS-Advanced'];
  25.       }
  26.  
  27.       if (choisedCoursies.includes("JS-Fundamentals") && choisedCoursies.includes("JS-Advanced") && choisedCoursies.includes("JS-Applications")) {
  28.          prices['JS-Advanced'] = 0.94 * prices['JS-Advanced'];
  29.          prices['JS-Fundamentals'] = 0.94 * prices['JS-Fundamentals'];
  30.          prices['JS-Applications'] = 0.94 * prices['JS-Applications']
  31.       }
  32.  
  33.       if (document.querySelectorAll('input[type=radio]')[1].checked) {
  34.          choisedCoursies.forEach(course => prices[course] = prices[course] - 0.06 * prices[course])
  35.       }
  36.  
  37.       price = choisedCoursies.reduce((totPrice, course) => (totPrice + prices[course]), 0);
  38.       price = Math.floor(price)
  39.  
  40.       if (choisedCoursies.includes("JS-Fundamentals") && choisedCoursies.includes("JS-Advanced") &&
  41.          choisedCoursies.includes("JS-Applications") && choisedCoursies.includes("JS-Web")) {
  42.          choisedCoursies.push('HTML and CSS')
  43.       }
  44.  
  45.       choisedCoursies.forEach(el => {
  46.          let ulElement = document.getElementsByClassName('courseBody')[1].querySelector('ul');
  47.          let liElement = document.createElement('li');
  48.          liElement.innerHTML = el;
  49.          ulElement.appendChild(liElement);
  50.       })
  51.       document.getElementsByClassName('courseFoot')[1].getElementsByTagName('p')[0].textContent = `Cost: ${price.toFixed(2)} BGN`
  52.    })
  53. }
Add Comment
Please, Sign In to add comment