Jump to content

[SOLVED] simple calculation form doesnt work!


Recommended Posts

Hi guys,

 

Im trying to make a simple calculation form...getting two numbera and an operator and calculate the selected operation but no success at all! my code is :

 


<script type="text/javascript">
function operate(){
    var number1  = document.getElementById('number1');
    var number2  = document.getElementById('number2');
    var operator = document.getElementById('operator');    
    
    if(number1.value != "") {
        if(operator.value = "+") {
            sum = number1.value + number2.value;
            alert(sum);
        } else if(operator.value = "-") {
            sum = number1.value - number2.value;
            alert(sum);
        } else if(operator.value = "*") {
            sum = number1.value * number2.value;
            alert(sum);
        }else if(operator.value = "/") {
            sum = number1.value / number2.value;
            alert(sum);
        }else if(operator.value = "%") {
            sum = number1.value % number2.value;
            alert(sum);
        } 
    }    
    else {
        alert("Please Fill All Fields With Proper Value");        
    }    
}
</script>

<input type='text' id='number1' />
<select name="operator" id="operator">
  <option value="+">+</option>
  <option value="-">-</option>
  <option value="*">*</option>
  <option value="/">/</option>
  <option value="%">%</option>
</select> 
<input type='text' id='number2' />  
<input type='button' onclick='operate()' value='GO' />

Link to comment
Share on other sites

OK I solved it  ;D  this maybe useful for other users :

 

<script type="text/javascript">
function operate(){
    var x  = document.getElementById('x').value;
    var y  = document.getElementById('y').value;
    var operator = document.getElementById('operator').value;    
    
    if(x.value != "") {
        if(operator == "+") {
            alert(+x + +y);
        } else if(operator == "-") {
            alert(+x - +y);
        } else if(operator == "*") {
            alert(+x * +y);
        }else if(operator == "/") {
            alert(+x / +y);
        }else if(operator == "%") {
            alert(+x % +y);
        } 
    }    
    else {
        alert("Please Fill All Fields With Proper Value");        
    }    
}
</script>

<input type='text' id='x' />
<select name="operator" id="operator">
  <option value="+">+</option>
  <option value="-">-</option>
  <option value="*">*</option>
  <option value="/">/</option>
  <option value="%">%</option>
</select> 
<input type='text' id='y' />  
<input type='button' onclick='operate()' value='GO' />

Link to comment
Share on other sites

thanks man...its now fully working :

 


<script type="text/javascript">
function operate(){
    var x  = document.getElementById('x').value;
    var y  = document.getElementById('y').value;
    var operator = document.getElementById('operator').value;    
    
    if((x != '') && (y != '')) {
        if(operator == "+") {
            alert(+x + +y);
        } else if(operator == "-") {
            alert(+x - +y);
        } else if(operator == "*") {
            alert(+x * +y);
        }else if(operator == "/") {
            alert(+x / +y);
        }else if(operator == "%") {
            alert(+x % +y);
        } 
    }    
    else {
        alert("Please Fill All Fields With Proper Value");        
    }    
}

function reset() {

}
</script>
<form name="values">
<input type='text' id='x' />
<select name="operator" id="operator">
  <option value="+">+</option>
  <option value="-">-</option>
  <option value="*">*</option>
  <option value="/">/</option>
  <option value="%">%</option>
</select> 
<input type='text' id='y' />  
<input type='button' onclick='operate()' value='GO' />
<input type="button" onclick="javascript:document.values.reset();return false" value="NEW">
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.