Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Global variables var prevCalc = 0; var calc = \"\"; //The following function d

ID: 3604522 • Letter: #

Question

//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!

Explanation / Answer

I found that the in the calculate function in order to add the present number to previous number you used a global variable called prevCalc to store the previous number but in the function you used it with another name called previousCalc so change that to to prevCalc to make it work.

//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 = prevCalc + num;
document.frmCalc.txtNumber.value = total;
}

}

function clear() {
document.frmCalc.txtNumber.value = "";
prevCalc = 0;
calc = "";
}
}

Comment for any further queries. Upvote if the answer is satisfactory.