items instanceof Array && items.length
let getDamage = function(first, second, health) {
const damage = Math.random() * 10 - 5;
console.log("result", damage)
if (damage < 0) {
console.log(`The player ${second.name} has blocked strike`)
}
return health -= (damage > 0) ? damage : 0
}
let firstFighter = {
name: "First"
}
let secondFighter = {
name: "Second"
}
let secondFighterHealth = 10;
let firstFighterHealth = 10;
let promise = new Promise(function(resolve, reject) {
let intervalId = setInterval(function() {
let timeoutId1 = setTimeout(function() {
secondFighterHealth = getDamage(firstFighter, secondFighter, secondFighterHealth);
if (secondFighterHealth <= 0) {
clearInterval(intervalId);
clearTimeout(timeoutId2);
resolve(firstFighter);
}
console.log('Second Health: ' + secondFighterHealth);
console.log('PAUSE!');
}, 250);
let timeoutId2 = setTimeout(function() {
firstFighterHealth = getDamage(secondFighter, firstFighter, firstFighterHealth);
if (firstFighterHealth <= 0) {
clearInterval(intervalId);
clearTimeout(timeoutId1);
resolve(secondFighter);
}
console.log('First Health: ' + firstFighterHealth);
console.log('PAUSE!');
}, 500);
}, 750);
});
promise.then(function(winner) {
console.log("Winner", winner);
});