-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile.js
More file actions
52 lines (30 loc) · 1005 Bytes
/
while.js
File metadata and controls
52 lines (30 loc) · 1005 Bytes
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
let vidaGoku = 100
let vidaSuperman = 100
const MIN_POWER = 5
const MAX_POWER = 12
const ambosSiguenVivos = () => vidaGoku >0 && vidaSuperman >0
const calcularGolpe = () => Math.random() * (MAX_POWER - MIN_POWER) + MIN_POWER
const gokuSigueVivo = () => vidaGoku > 0
let round = 0
while (ambosSiguenVivos()) {
round++
console.log(`Round ${round}`)
const golpeGoku = calcularGolpe()
const golpeSuperman = calcularGolpe()
if (golpeGoku > golpeSuperman){
// ataca Goku
console.log(`Goku ataca a Superman con un golpe de ${golpeGoku}`)
vidaSuperman -= golpeGoku
console.log(`Superman queda en ${vidaSuperman} de vida`)
} else {
// ataca Superman
console.log(`Super ataca a Goku con un golpe de ${golpeSuperman}`)
vidaGoku -= golpeSuperman
console.log(`Goku queda en ${vidaGoku} de vida`)
}
}
if (gokuSigueVivo()){
console.log(`Goku ganó la pelea. Su vida es de: ${vidaGoku}`)
} else {
console.log(`Superman ganó la pelea. Su vida es de ${vidaSuperman}`)
}