Question: //Global variables var prevCalc = 0; var calc = ; //The following function displays a number in the textfield when a number is clicked. //Note
//Global variables
var prevCalc = 0;
var calc = "";
//The following function displays a number in the textfield when a number is clicked.
//Note that it keeps concatenating numbers which are clicked.
function showNum(value) {
document.frmCalc.txtNumber.value += value;
}
//The following function decreases the value of displayed number by 1.
//isNaN method checks whether the value passed to the method is a number or not.
function decrement() {
var num = parseFloat(document.frmCalc.txtNumber.value);
if (!(isNaN(num))) {
num--;
document.frmCalc.txtnumber.value = num;
}
}
//The following function is called when "Add" button is clicked.
//Note that it also changes the values of the global variables.
function add() {
var num = parseFloat(document.frmCalc.txtNumber.value);
if (!(isNaN(num))) {
prevCalc = num;
document.frmCalc.txtNumber.value = "";
calc = "Add";
}
}
//The following function is called when "Calculate" button is clicked.
//Note that this function is dependent on the value of global variable.
function calculate() {
var num = parseFloat(document.frmCalc.txtNumber.value);
if (!(isNaN(num))) {
if (calc == "Add"){
var total = previousCalc + num;
document.frmCalc.txtNumber.value = total;
}
}
function clear() {
document.frmCalc.txtNumber.value = "";
prevCalc = 0;
calc = "";
}
}
This is code that I was given. I am supposed to fix the synax errors which I have done. However, the add function does not seem to work when I implement this calculator. Can someone take a look at the add function and see why it is not adding two numbers together. In javascript. Thanks so much!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
