I am very new and practicing functions right now
I made this to create a simple remaining calories calculator, but I wanted to ask:
When I have originally created a value that already calculates the difference between, lets say, caloriesMen and caloriesToday and named it caloriesLeft (so I literally wrote ' let caloriesLeft = caloriesMen - caloriesToday ') but it came up with NaN which is somewhat understandable.
But when I did that in the alert() it understood what I wanted (e.g. 'You have ' + (caloriesMen - caloriesToday) + ' calories left to consume today!')
-
Could you tell me why is that?
-
How would you made this code (better/cleaner)?
My code and vars:
let gender;
let caloriesMen = 2500;
let caloriesWomen = 2000;
let caloriesToday;
function calCalc() {
gender = prompt('Are you man or woman?')
if (gender == 'man') {
caloriesToday = prompt('How many calories have you already consumed today?');
alert('You have ' + (caloriesMen - caloriesToday) + ' calories left to consume today!');
} else if (gender == 'woman') {
caloriesToday = prompt('How many calories have you already consumed today?');
alert('You have ' + (caloriesWomen - caloriesToday) + ' calories left to consume today!');
}
}
calCalc();
Thank you!
Aucun commentaire:
Enregistrer un commentaire