Hi. I’m not sure whether it’s allowed to post non-challenge / project related questions here but here goes.
I’m trying to get a variable (with it’s values) out of a function but nothing I’m doing seems to be working. I’m thinking the function might not be structured correctly for this to work but I don’t know how to fix it. Below is the function, its variables and my attempt at calling the function. Calling it gives me this error though: Uncaught TypeError: Cannot read properties of undefined (reading ‘target’)
let outputMbKwh = document.getElementById('outputMbKwh');
let inputMonthlyBill = document.getElementById('inputMonthlyBill');
var monthlyBillToKwhConversion = 0;
function calcMbKwh(event) {
inputMonthlyBill = event.target.value;
inputMonthlyBill.innerHTML = inputMonthlyBill;
if (inputMonthlyBill < maxCeilingDiscountedBracketInRands) {
monthlyBillToKwhConversion = inputMonthlyBill / higherTarrif
} else if (inputMonthlyBill > maxCeilingDiscountedBracketInRands) {
monthlyBillToKwhConversion = Math.round(((inputMonthlyBill - maxCeilingDiscountedBracketInRands) / higherTarrif) + (maxCeilingDiscountedBracketInRands / discountedTarrif))
}
outputMbKwh.innerHTML = monthlyBillToKwhConversion;
return (monthlyBillToKwhConversion);
}
let monthlyBillToKwhConversion2 = calcMbKwh(event);
console.log('monthlyBillToKwhConversion', monthlyBillToKwhConversion);
The value of the variable I’m calling above is showing up in the form that goes with this but since I can’t get it out of the function I can’t use it in other calculations. Just the fisrt form field works when I call the variable in that way as well. Full code can be seen in the codepen here
Sorry if it’s a disaster, I’m still very much a beginner