-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14-while.js
More file actions
34 lines (25 loc) · 840 Bytes
/
14-while.js
File metadata and controls
34 lines (25 loc) · 840 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
//Programa que toma a un sujeto y calcula cuando ya ha bajado 3 kg de peso como mínimo.
var ivan = {
nombre: 'ivan',
apellido: 'Espadas',
edad: 28,
peso: 75
};
const AUMENTO_PESO = .2;
console.log(`Al inicio del año ${ivan.nombre} pesa ${ivan.peso} kg`);
const aumentarDePeso = (persona) => (persona.peso += AUMENTO_PESO);
const adelgazar = (persona) => (persona.peso -= AUMENTO_PESO);
const comeMucho = () => Math.random() < 0.3;
const realizaDeporte = () => Math.random() < 0.4;
const META = ivan.peso - 3;
var dias = 0;
while (ivan.peso > META) {
if (comeMucho()) {
aumentarDePeso(ivan);
}
if (realizaDeporte()) {
adelgazar(ivan);
}
dias += 1;
}
console.log(`Pasaron ${dias} dias para que ${ivan.nombre.apellido} llegara a su meta de tener ${ivan.peso.toFixed(1)} kg`);