I need help with my calculator app. When the calculator pops up it doesn't display the numbers when the keys are pressed.
When I inspect element and go to console it says that:
Uncaught ReferenceError: answer is not defined
Uncaught ReferenceError: backspace is not defined
Uncaught ReferenceError: addtoscreen is not defined
How would I fix the code so that the screen displays the numbers? Thanks in advance.
CSS CODECode
#calculator {
width: 250px;
height: 350px;
border: 5px solid black;
text-align: center;
background: lightblue;
margin: 150px auto;
}
#display {
margin-top: 30px;
margin-bottom: 20px;
width: 220px;
height: 30px;
border: 1px solid red;
text-align: right;
font: 20px bold;
color: black;
}
#keys {
width: 41px;
height: 35px;
margin-left: 10px;
margin-bottom: 20px;
cursor: pointer;
}
#keys:hover {
background: yellow;
font-weight: bold;
}
#equal {
width: 41px;
height: 35px;
margin-left: 10px;
margin-bottom: 20px;
cursor: pointer;
}
#equal:hover {
background: yellow;
font-weight: bold;
}
HTML CODECode
<head>
<title>Calculator</title>
</head>
<body>
{{> calculator}}
</body>
<template name="calculator">
<div id="calculator">
<form>
<input type="text" id="display" disabled><br>
<input type="button" value="<--" id="keys" onClick="backspace()">
<input type="button" value="C" id="keys" onClick="addtoscreen('c')">
<input type="button" value="%" id="keys" onClick="addtoscreen('%')">
<input type="button" value="MR" id="keys" onClick="addtoscreen('MR')"><br>
<input type="button" value="7" id="keys" onClick="addtoscreen('7')">
<input type="button" value="8" id="keys" onClick="addtoscreen('8')">
<input type="button" value="9" id="keys" onClick="addtoscreen('9')">
<input type="button" value="/" id="keys" onClick="addtoscreen('/')"><br>
<input type="button" value="4" id="keys" onClick="addtoscreen('4')">
<input type="button" value="5" id="keys" onClick="addtoscreen('5')">
<input type="button" value="6" id="keys" onClick="addtoscreen('6')">
<input type="button" value="*" id="keys" onClick="addtoscreen('*')"><br>
<input type="button" value="1" id="keys" onClick="addtoscreen('1')">
<input type="button" value="2" id="keys" onClick="addtoscreen('2')">
<input type="button" value="3" id="keys" onClick="addtoscreen('3')">
<input type="button" value="-" id="keys" onClick="addtoscreen('-')"><br>
<input type="button" value="0" id="keys" onClick="addtoscreen('0')">
<input type="button" value="." id="keys" onClick="addtoscreen('.')">
<input type="button" value="+" id="keys" onClick="addtoscreen('+')">
<input type="button" value="=" id="equal" onClick="answer()">
</form>
</div>
</template>
JAVASCRIPT CODECode
if (Meteor.isClient) {
var x;
var box=document.getElementById('nums');
Template.calculator.events({
'click keys': function addtoscreen(x) {
box.value +=x;
if(x=='c') {
box.value='';
}else if (x=='<--') {
var number=box.value;
var len=number.length-1;
var newnumber=number.substring(0,len);
box.value=newnumber;
}
},
'click equal': function answer() {
x=box.value;
x=eval(x);
box.value=x;
}
});
}
