nikolayneykov

Untitled

May 27th, 2019
178
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 firstPart = arr.slice(0, arr.length / 2)
  9.   let secondPart = arr.slice(arr.length / 2)
  10.  
  11.   let firstChunks = []
  12.   let secondChunks = []
  13.  
  14.   while (firstPart.length > 0) {
  15.     firstChunks.push(firstPart.splice(0, num))
  16.     secondChunks.push(secondPart.splice(0, num))
  17.   }
  18.  
  19.   let firstGiant = firstChunks
  20.     .map(x => x.reduce((acc, el) => acc * el))
  21.     .reduce((acc, el) => acc + el, 0)
  22.  
  23.   let secondGiant = secondChunks
  24.     .map(x => x.reduce((acc, el) => acc * el))
  25.     .reduce((acc, el) => acc + el, 0)
  26.  
  27.   if (damagePerHit !== 0) {
  28.     while (firstGiant > stopNumber && secondGiant > stopNumber) {
  29.       firstGiant -= damagePerHit
  30.       secondGiant -= damagePerHit
  31.       rounds++
  32.     }
  33.   }
  34.  
  35.   console.log(
  36.     firstGiant === secondGiant
  37.       ? `Its a draw ${firstGiant} - ${secondGiant}`
  38.       : firstGiant > secondGiant
  39.         ? `First Giant defeated Second Giant with result ${firstGiant} - ${secondGiant} in ${rounds} rounds`
  40.         : `Second Giant defeated First Giant with result ${secondGiant} - ${firstGiant} in ${rounds} rounds`
  41.   )
  42. }
Advertisement
Add Comment
Please, Sign In to add comment