I'm rather new to Javascript, and I'm trying to calculate the BMR in "Oppgave A" of a male who is 18 years old and 180 centimeters high. The first function of the calculation works. I get the amount of calories this male with a user-defined weight needs each hour.
Problem: When trying to calculate the energy used in sitting at the desk bmr * 1.2, the previous variable bmr i used in the function printBmr from the function turns out is undefined when I call it in the new function printPalStille.
Question: How can I use the variable bmr from the previous function printBmr and use it in the new function *printPalStille *under the new variabel pal?
The Code:
<script>
// __________________________________________________
// Oppgave A
var vekt = document.getElementById("idtxtVekt");
var svar = document.getElementById("idpSvar");
vekt.focus();
vekt.addEventListener("keyup", printBmr);
function printBmr (event) {
if (event.keyCode === 13) {
var kalkuler = 35.27 + (0.558 * vekt.value);
var bmr = Math.floor(kalkuler);
svar.innerHTML="Energimengden som kreves hver time for deg er: " + bmr + " kalorier i timen.";
}
}
// __________________________________________________
// Oppgave B
var palStille = document.getElementById("idbtnStille");
var palTur = document.getElementById("idbtnTur");
var svarPal = document.getElementById("idpsvarPal");
palStille.addEventListener("click", printPalStille);
// palTur.addEventListener("click", printPalTur);
function printPalStille (event) {
var pal = bmr.value * 1.2;
svarPal.innerHTML = "Din pal er: " + pal.value;
console.log(bmr);
}
// __________________________________________________
</script>
I am a complete beginner and I'm trying to improve my javascript skills, but sometimes things get too complicated for me to understand within forums and communities, therefore I have problems troubleshooting this from other questions, even though the answer is there, and i apologise for the use of Norwegian phrases and words with-in my code :(