Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr, num) {
- if (arr.length === 0) {
- throw new Error()
- }
- let [damagePerHit, stopNumber, rounds] = [
- Math.min(...arr),
- Math.max(...arr),
- 1
- ]
- let products = arr
- .slice(0, arr.length / num)
- .reduce((acc, el, i) => {
- acc.push(arr.splice(0, num))
- return acc
- }, [])
- .map(x => x.reduce((acc, el) => acc * el, 1))
- let [firstGiant, secondGiant] = [
- products.slice(0, products.length / 2).reduce((acc, el) => acc + el, 0),
- products.slice(products.length / 2).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