nikolayneykov

Untitled

May 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve () {
  2.   let [p1Span, p2Span] = document.querySelectorAll(
  3.     '#result span:nth-child(odd)'
  4.   )
  5.  
  6.   let history = document.querySelector('#history')
  7.  
  8.   let cards = Array.from(document.querySelectorAll('img'))
  9.   cards.forEach((c, i) => {
  10.     c.addEventListener('click', compareCards)
  11.     c.p1 = i < 8
  12.     c.p2 = i >= 8
  13.   })
  14.  
  15.   function compareCards () {
  16.     this.src = './images/whiteCard.jpg'
  17.  
  18.     if (this.p1) {
  19.       p1Span.textContent = this.name
  20.     } else if (this.p2) {
  21.       p2Span.textContent = this.name
  22.     }
  23.  
  24.     if (p1Span.textContent && p2Span.textContent) {
  25.       let p1Card = cards.find(c => c.p1 && c.name === p1Span.textContent)
  26.       let p2Card = cards.find(c => c.p2 && c.name === p2Span.textContent)
  27.  
  28.       if (+p1Card.name > +p2Card.name) {
  29.         p1Card.style.border = '2px solid green'
  30.         p2Card.style.border = '2px solid red'
  31.       } else {
  32.         p2Card.style.border = '2px solid green'
  33.         p1Card.style.border = '2px solid red'
  34.       }
  35.  
  36.       history.textContent += `[${p1Span.textContent} vs ${p2Span.textContent}] `
  37.       p1Span.textContent = ''
  38.       p2Span.textContent = ''
  39.     }
  40.   }
  41. }
Add Comment
Please, Sign In to add comment