I can't seem to understand why I can't get .toFixed()
to work in my code. I've tried different variations but something is missing.
I get the following error upon running the code.
Uncaught TypeError: document.getElementById(...).value.toFixed is not a function
My Code:
<input type="number" id="num1">
<p>+</p>
<input type="number" id="num2">
<p>=</p>
<p id="result"></p>
<br>
<button id="submit" onclick="calculate()">Calculate</button>
<script>
function calculate(){
const a = document.getElementById("num1").value;
const b = document.getElementById("num2").value;
let finalAnswer = a.toFixed(2) + b.toFixed(2);
document.getElementById("result").innerHTML = finalAnswer;
console.log(a);
}
</script>