-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtwo-fighters-one-winner.js
49 lines (40 loc) · 1.46 KB
/
two-fighters-one-winner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Pokemon {
constructor(name, health, damagePerAttack) {
this.name = name;
this.health = health;
this.damagePerAttack = damagePerAttack;
}
takeDamage(pokemon) {
this.health -= pokemon.damagePerAttack;
if (this.health < 0) {
this.health = 0;
}
}
hasFainted() {
return this.health <= 0;
}
}
function declareWinner(pokemon1, pokemon2) {
let currentAttacker = pokemon1;
let currentDefender = pokemon2;
while (!currentDefender.hasFainted() && !currentAttacker.hasFainted()) {
currentDefender.takeDamage(currentAttacker);
if (currentDefender.hasFainted()) {
return currentAttacker.name;
}
[currentAttacker, currentDefender] = [currentDefender, currentAttacker];
}
}
function declareWinner(pokemon1, pokemon2) {
let currentAttacker = pokemon2;
let currentDefender = pokemon1;
do {
[currentAttacker, currentDefender] = [currentDefender, currentAttacker];
currentDefender.takeDamage(currentAttacker);
} while (!currentDefender.hasFainted() && !currentAttacker.hasFainted());
return currentAttacker.name;
}
console.log(declareWinner(new Pokemon('Mew', 10, 2), new Pokemon('Zapdos', 5, 4)), 'Mew');
console.log(declareWinner(new Pokemon('Clefairy', 20, 5), new Pokemon('Zapdos', 5, 4)), 'Clefairy');
console.log(declareWinner(new Pokemon('Clefairy', 20, 5), new Pokemon('Zapdos', 5, 4)), 'Clefairy');
console.log(declareWinner(new Pokemon('Jerry', 30, 3), new Pokemon('Clefairy', 20, 5)), 'Clefairy');