function calc(action){
var x=document.getElementById(“tb1”).value ;
var y=document.getElementById(“tb2”).value ;
if (isNaN(x))
{alert(“Please re-enter value for first number”);}
if (isNaN(y))
{alert(“Please re-enter value for second number”);}
switch (action) {
case ‘’ : ans = xy; break;
case ‘+’ : ans = x+y; break;
case ‘-’ : ans = x-y; break;
case ‘/’ : {if (y==0)
{alert(“Cannot divide by zero.”);}
else
{ ans = x/y;} break;} // still need division by zero check
default : alert(action+’ is invalid operation’);
}
}
//problem is here
[COLOR=“Red”]function evalit(){
var selectedButton = “NO BUTTON”;
var inputElements = document.getElementsByName(“radio_button”);
var inputElementCount = inputElements.length;
for (i=0;i<inputElementCount;i++){
var currentElement=inputElements[i];
if (!(currentElement.checked)){alert(“Please select operater”);
break; }
I think you’re making this more complicated than it needs to be.
you don’t need 2 functions. you can do it all with 1.
I would remove the event handlers from the radio buttons
when the “Calculate” button is clicked all you need to do is
3a) get the 2 numbers in the textboxes and convert them from a string to numbers using Number()
3b) find the checked “operator” radio button by looping through the radio buttons
3c) perform the appropriate operation on the 2 numbers in 1) based on the checked radio button in 3b)
3d) display the result.
All of the steps in 3) can be done using 1 function.