Advertisement
bobo_bobkata

Untitled

Oct 14th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. function solve() {
  2. let priceMap = {
  3. "js-fundamentals": 170,
  4. "js-advanced": 180,
  5. "js-applications": 190,
  6. "js-web": 490,
  7. };
  8.  
  9. let listCourses = document.querySelector("#myCourses > div.courseBody > ul");
  10. document.querySelector("#availableCourses > div.courseFoot > button").addEventListener("click", () => {
  11. let type = "";
  12. let selected = [];
  13. Array
  14. .from(document.querySelectorAll("input"))
  15. .filter(el => el.checked === true)
  16. .forEach(el =>
  17. el.value.includes("js") ? selected.push(el.value) : type = el.value
  18. );
  19.  
  20. let sum = getPrice(selected);
  21. let price = Math.floor(sum);
  22.  
  23. let priceForm = document.querySelector("#myCourses > div.courseFoot > p");
  24. let curPrice = Number(priceForm.innerHTML.split(" ")[1]);
  25. priceForm.innerHTML = `Cost: ${curPrice + price}.00 BGN`;
  26.  
  27. function getPrices(type) {
  28. if (type === "online") {
  29. priceMap["js-fundamentals"] = priceMap["js-fundamentals"] * 0.94;
  30. priceMap["js-advanced"] = priceMap["js-advanced"] * 0.94;
  31. priceMap["js-applications"] = priceMap["js-applications"] * 0.94;
  32. priceMap["js-web"] = priceMap["js-web"] * 0.94;
  33. }
  34. }
  35.  
  36. function transformName(courseName) {
  37. let tokens = courseName.split("-");
  38. let fPart = tokens[0].toUpperCase();
  39. let name = fPart + "-" + String.fromCharCode(tokens[1].charCodeAt(0) - 32);
  40. for (let i = 1; i < tokens[1].split("").length; i++) {
  41. name += tokens[1].charAt(i);
  42. }
  43. return name;
  44. }
  45.  
  46. function addToCourses(courseName) {
  47. if (courseName !== "HTML and CSS") {
  48. courseName = transformName(courseName);
  49. }
  50. let applications = document.createElement("li");
  51. applications.innerHTML = courseName;
  52. listCourses.appendChild(applications);
  53. }
  54.  
  55. function getPrice(selected) {
  56. getPrices(type);
  57. let arrCopy = selected.slice(0, selected.length);
  58. let price = 0;
  59. if (selected.indexOf("js-fundamentals") !== -1 && selected.indexOf("js-advanced") !== -1) {
  60. price += priceMap[selected.shift()] + (priceMap[selected.shift()] * 0.90);
  61. if (selected.indexOf("js-applications") !== -1) {
  62. price += priceMap[selected.shift()];
  63. price *= 0.94;
  64. }
  65. if (selected.length === 1) {
  66. price += priceMap[selected.shift()];
  67. arrCopy.push('HTML and CSS');
  68. }
  69. }
  70. while (selected.length > 0) {
  71. price += priceMap[selected.shift()];
  72. }
  73. arrCopy.forEach(el => addToCourses(el));
  74. return price;
  75. }
  76. }
  77. );
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement