I need help in code of Calculator

Can you anyone help me

I have assignment calculator by javascript user enter first and second number and choice one operater if not select operator alert to select operater

<?xml version = “1.0” encoding = “utf-8” ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en”>
<head> <link rel=“stylesheet” type=“text/css” href=“Ass3.css”/>
<title>My Calcultor</title>

<script type=“text/javascript”>
var ans ;

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; }

else
{document.getElementById(“tb3”).value= ans;}
}

}[/COLOR]

</script > </head>

<body>

<h1>My Calcultor</h1>
<div id=“form”>
<form method=“post” action=“”>
<p>First number: <input type=“text” name=“input” size=“10” id=“tb1”/></p>
<p>Second number: <input type=“text” name=“input” size=“10” id=“tb2”/></p>
<p><input type=“radio” name=“radio_button” value=“+” checked = “checked” onclick=“calc(this.value)”/>Addition<input type=“radio” name=“radio_button” value=“-” checked = “checked” onclick=“calc(this.value)”/>Subtraction<input type=“radio” name=“radio_button” value=“*” checked = “checked” onclick=“calc(this.value)”/>Multiplication<input type=“radio” name=“radio_button” value=“/” checked = "checked"onclick=“calc(this.value)”/>Division</p>

<p><input type=“button” class=“button” value=“Calculate” name=“Calculate” onclick=“evalit()”/></p>
<p>Result<input type=“text” size=“10” name=“Result” id=“tb3”/></p>

</form>
</div>
</body>
</html>

I think you’re making this more complicated than it needs to be.

  1. you don’t need 2 functions. you can do it all with 1.

  2. I would remove the event handlers from the radio buttons

  3. 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.

perhaps something like this.

but I haven’t included any data validation or eror checking.

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head> <link rel="stylesheet" type="text/css" href="Ass3.css"/>
<title>My Calcultor</title>
<script  type="text/javascript">
 
function calc() {
    //get the 2 numbers
    var num1 = new Number(document.getElementById("txtNum1").value);
    var num2 = new Number(document.getElementById("txtNum2").value);
 
 //find the checked radio button
    for(var i=0; i < radBtns.length; i++) {
            if(radBtns[i].checked) {
                 var op = radBtns[i].value;
                 i = radBtns.length;
             }
    }
 
    //perform the calculation
    switch(op) {
        case '+':
           document.getElementById("txtResults").value = num1 + num2;
        break;
        case '-':
            document.getElementById("txtResults").value = num1 - num2;
        break;
        case '*':
            document.getElementById("txtResults").value = num1 * num2;
        break;
        case '/':
            document.getElementById("txtResults").value = num1 / num2;
        break; 
    } 
}
 
var radBtns = new Array();
window.onload=function() {
 radBtns = document.getElementsByName("radOp");
}
 
</script>
</head>
<body>
<h1>My Calcultor</h1>
 
<div id="form">
<form method="post" action="">
<p>First number: <input type="text" name="input" size="10" id="txtNum1"/></p>
<p>Second number: <input type="text" name="input" size="10" id="txtNum2"/></p>
<p>
 <input type="radio" name="radOp" value="+" />Addition
 <input type="radio" name="radOp" value="-"/>Subtraction
 <input type="radio" name="radOp" value="*"/>Multiplication
 <input type="radio" name="radOp" value="/" />Division
</p>
<p><input type="button" class="button" value="Calculate" name="Calculate" onclick="calc()"/></p>
<p>Result<input type="text" size="10"  id="txtResults" readonly="readonly" /></p>
</form>
</div>
</body>
</html>