Advertisement
nikolayneykov

Untitled

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