forked from omiras/solved-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
good-vs-evil.js
53 lines (44 loc) · 1.36 KB
/
good-vs-evil.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
50
51
52
53
//creamos un array donde guardamos el "worth" de cada raza
const goodWorth = [
1, // Hobbits
2, // Men
3, // Elves
3, // Dwarves
4, // Eagles
10, // Wizards
];
const evilWorth = [
1, // Orcs
2, // Men
2, // Wargs
2, // Goblins
3, // Uruk Hai
5, // Trolls
10, // Wizards
];
//función para calcular worth de cada lado:good y evil
function calculateWorth(points, worth) {
// y puntos y worth?
let warriors = points.split(" ");
warriors = warriors.map(element => {
return Number(element)
})
//console.log("Warriors enteros: ", warriors);
// multiplicar el número que hay de cada uno de los guerreros por su "worth" (fuerza)
let totalPower = 0
warriors.forEach( (warrior, index) => {
totalPower += warrior * worth[index]
})
console.log(totalPower)
return totalPower
}
//función para calcular qué lado gana
function goodVsEvil(goodCount, evilCount) {
const goodTotalWorth = calculateWorth(goodCount, goodWorth);
const evilTotalWorth = calculateWorth(evilCount, evilWorth);
if (evilTotalWorth > goodTotalWorth)
return "Battle Result: Evil eradicates all trace of Good";
else if (goodTotalWorth > evilTotalWorth)
return "Battle Result: Good triumphs over Evil";
return "Battle Result: No victor on this battle field";
}