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

}

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

Can someone look at this code in javascript. The decrement function does not work properly and calculate (n-1). I also cannot figure out how to implement a subtract function and square root function. Thanks so much.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!