Advertisement
ilianrusev

Untitled

Feb 18th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. function solve() {
  2. const getInputField = n =>
  3. document.querySelector(`#container > input[type=text]:nth-child(${n})`)
  4. const inputs = [getInputField(1), getInputField(2), getInputField(3)]
  5. const html = {
  6. moviesList: document.querySelector("#movies > ul"),
  7. archivesList: document.querySelector("#archive > ul"),
  8. }
  9.  
  10. const checkValidInput = (arr, num) =>
  11. arr.every(x => x !== "") && !isNaN(Number(num))
  12. const clearInputs = arr => arr.map(x => (x.value = ""))
  13.  
  14. function onScreenTemplate(n, h, p) {
  15. const wrapper = document.createElement("li")
  16.  
  17. wrapper.innerHTML = `<span>${n}</span><strong>Hall: ${h}</strong>
  18. <div><strong>${p.toFixed(2)}</strong><input placeholder="Tickets Sold"/>
  19. <button>Archive</button></div>`
  20.  
  21. return wrapper
  22. }
  23.  
  24. function archivedTemplate(n, p) {
  25. const wrapper = document.createElement("li")
  26.  
  27. wrapper.innerHTML = `<span>${n}</span>
  28. <strong>Total amount: ${p.toFixed(2)}</strong>
  29. <button>Delete</button>`
  30.  
  31. return wrapper
  32. }
  33.  
  34. document.addEventListener("click", e => {
  35. e.preventDefault()
  36.  
  37. if (e.target.tagName === "BUTTON") {
  38. const [n, h, p] = inputs.map(x => x.value)
  39.  
  40. const buttons = {
  41. "On Screen": () => {
  42. if (checkValidInput([n, h, p], p)) {
  43. clearInputs(inputs)
  44. html.moviesList.appendChild(
  45. onScreenTemplate(n, h, Number(p))
  46. )
  47. }
  48. },
  49. Archive: e => {
  50. const ticketsSold = e.previousElementSibling.value
  51.  
  52. if (checkValidInput([ticketsSold], ticketsSold)) {
  53. const parent = e.parentNode.parentNode
  54. const name = parent.children[0].innerHTML
  55. const price =
  56. e.previousElementSibling.previousElementSibling
  57. .innerHTML
  58.  
  59. html.archivesList.appendChild(
  60. archivedTemplate(name, ticketsSold * Number(price))
  61. )
  62. parent.remove()
  63. }
  64. },
  65. Delete: e => e.parentNode.remove(),
  66. Clear: () => (html.archivesList.innerHTML = ""),
  67. }
  68.  
  69. buttons[e.target.textContent](e.target)
  70. }
  71. })
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement