Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr, num) {
- let [damagePerHit, stopNumber, rounds] = [
- Math.min(...arr),
- Math.max(...arr),
- 1
- ]
- let [firstPart, secondPart, firstGiant, secondGiant] = [
- arr.slice(0, arr.length / 2),
- arr.slice(arr.length / 2),
- [],
- []
- ]
- while (firstPart.length > 0) {
- firstGiant.push(firstPart.splice(0, num).reduce((acc, el) => acc * el))
- secondGiant.push(secondPart.splice(0, num).reduce((acc, el) => acc * el))
- }
- firstGiant = firstGiant.reduce((acc, el) => acc + el, 0)
- secondGiant = secondGiant.reduce((acc, el) => acc + el, 0)
- if (damagePerHit !== 0) {
- while (firstGiant > stopNumber && secondGiant > stopNumber) {
- firstGiant -= damagePerHit
- secondGiant -= damagePerHit
- rounds++
- }
- }
- console.log(
- firstGiant === secondGiant
- ? `Its a draw ${firstGiant} - ${secondGiant}`
- : firstGiant > secondGiant
- ? `First Giant defeated Second Giant with result ${firstGiant} - ${secondGiant} in ${rounds} rounds`
- : `Second Giant defeated First Giant with result ${secondGiant} - ${firstGiant} in ${rounds} rounds`
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement