Advertisement
nikolayneykov

Untitled

May 28th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve () {
  2.   Array.from(document.querySelectorAll('.seat')).forEach(s =>
  3.     s.addEventListener('click', takeSeat)
  4.   )
  5.  
  6.   document
  7.     .querySelector('#summary button')
  8.     .addEventListener('click', printSummary)
  9.  
  10.   let zones = Array.from(document.querySelectorAll('section'))
  11.   let sectors = ['A', 'B', 'C']
  12.   let output = document.querySelector('#output')
  13.   let [fans, profit] = [0, 0]
  14.  
  15.   let prices = {
  16.     levski: [10, 7, 5],
  17.     litex: [10, 7, 5],
  18.     vip: [25, 15, 10]
  19.   }
  20.  
  21.   function takeSeat () {
  22.     let seat = this.textContent
  23.     let zone = zones.find(x =>
  24.       [...x.querySelectorAll('.seat')].some(s => s === this)
  25.     ).className
  26.     let sector = sectors[this.parentNode.cellIndex]
  27.     if (this.isTaken) {
  28.       output.value += ` Seat ${seat} in zone ${zone} sector ${sector} is unavailable.\n`
  29.     } else {
  30.       output.value += ` Seat ${seat} in zone ${zone} sector ${sector} was taken.\n`
  31.       this.style.background = 'rgba(255, 0, 0)'
  32.       this.isTaken = true
  33.       fans++
  34.       profit += prices[zone.toLowerCase()][this.parentNode.cellIndex]
  35.     }
  36.   }
  37.  
  38.   function printSummary () {
  39.     this.nextElementSibling.textContent = `${profit} leva, ${fans} fans.`
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement