nikolayneykov

Untitled

May 27th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (arr, num) {
  2.   let [damagePerHit, stopNumber, rounds] = [
  3.     Math.min(...arr),
  4.     Math.max(...arr),
  5.     1
  6.   ]
  7.  
  8.   let products = arr
  9.     .slice(0, arr.length / 2 + 1)
  10.     .reduce((acc, el, i) => {
  11.       acc.push(arr.splice(0, num))
  12.       return acc
  13.     }, [])
  14.     .filter(x => x.length > 0)
  15.     .map(x => x.reduce((acc, el) => acc * el, 1))
  16.  
  17.   let [firstGiant, secondGiant] = [
  18.     products.slice(0, products.length / 2).reduce((acc, el) => acc + el, 0),
  19.     products.slice(products.length / 2).reduce((acc, el) => acc + el, 0)
  20.   ]
  21.  
  22.   if (damagePerHit !== 0) {
  23.     while (firstGiant > stopNumber && secondGiant > stopNumber) {
  24.       firstGiant -= damagePerHit
  25.       secondGiant -= damagePerHit
  26.       rounds++
  27.     }
  28.   }
  29.  
  30.   console.log(
  31.     firstGiant === secondGiant
  32.       ? `Its a draw ${firstGiant} - ${secondGiant}`
  33.       : firstGiant > secondGiant
  34.         ? `First Giant defeated Second Giant with result ${firstGiant} - ${secondGiant} in ${rounds} rounds`
  35.         : `Second Giant defeated First Giant with result ${secondGiant} - ${firstGiant} in ${rounds} rounds`
  36.   )
  37. }
Advertisement
Add Comment
Please, Sign In to add comment