Advertisement
nikolayneykov

Untitled

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