I was trying to declare variables from two input fields that I would reuse for several functions in a basic calculator. I keep getting an error message from the console that it can’t read the value of null. I was hoping to avoid copy pasting the same declarations inside each function.
Codepen: https://codepen.io/alltheintegers/pen/BqMeGG
//var n1 = 4;
//var n2 = 2;
var n1 = document.getElementById(‘num1’).value
var n2 = document.getElementById(‘num2’).value
function divide(){
var quotient = n1/n2;
document.getElementById(“result”).value = quotient;
}
function sub(){
var difference = n1 - n2;
document.getElementById(‘result’).value = difference;
}
Blockquote