function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh M 20Rakesh M 20 

created calculator and trying to perform all math operation

I am Trying to Perform Mathoperations in Calculator but its not working,can anyone help me to fix this issue.
Here's My code
VF Page
<apex:page controller="Caluclator">
   <script type="text/javascript">
 function textFunc() 
 {
 var num1 = document.getElementById('n1').value;
 var num2 = document.getElementById('n2').value;
 
     String op;
 
 Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.Caluclator.showText}', num1,num2,op,
 function(result, event)
 {
     debugger;
 if(event.status) 
 {
 // This is the way to get VF tag id to map result returned from call back function parameter result
 
 document.getElementById("{!$Component.pb.pbs.pbsi.numone}").innerHTML = result
 System.debug('Result--'+result);
 }
{escape: true}
 }); 
 }
 </script>
 Number 1 :<input id="n1" type="Number" />
 Number 2 :<input id="n2" type="Number" />
 <button type="button" onclick="textFunc('add')">Add</button>
 <button type="button" onclick="textFunc('sub')">Sub</button>
 <button type="button" onclick="textFunc('mul')">Mul</button>
 <button type="button" onclick="textFunc('div')">Div</button>
 <button type="button" onclick="textFunc('mod')">Mod</button>
 <button type="button" onclick="textFunc('perc')">Perc</button>
 <apex:pageBlock id="pb">
 <apex:pageBlockSection id="pbs">
 <apex:pageBlockSectionItem id="pbsi">
 <apex:outputText id="numone"/>   
     </apex:pageBlockSectionItem> 
     </apex:pageBlockSection>
 </apex:pageBlock>
</apex:page>

Controller :
public class Caluclator {
    Public Integer num1 {get;set;}
    Public Integer num2 {get;set;}
    Public String fun {get;set;}
    
    @RemoteAction
    public static Integer showText(Integer num1,Integer num2,String fun) 
    {           integer result=0;
          //return num1 + num2;  
          if(fun == 'add')
        {
            result = num1 + num1;
        }
        else if(fun == 'sub')
        {
             result = num1 - num1;
        }
         else if(fun == 'mul')
        {
             result = num1 * num1;
        }
        else if(fun == 'div')
        {
             result = num1 / num1;
        }
         else if(fun == 'mod')
        {
             result =  math.mod(Integer.valueOf(num1) , Integer.valueOf(num2));    
        }
         else if(fun == 'perc')
        {
            result = (num1/num2) * 100;
        }
     
        return result;
    }
}
AbhishekAbhishek (Salesforce Developers) 
Yes you can do this on VF page.
 
Try the below code as reference:
<apex:page controller="Calculator">
<apex:form >
<apex:inputText value="{!number1}"/>
<apex:inputText value="{!number2}"/>
<apex:inputText value="{!operator}"/>
<apex:commandButton value="ShowResult" action="{!calculation}">{!result}</apex:commandbutton>
</apex:form>
</apex:page>
//////////////// Controller /////////////////
public class Calculator
{
public integer number1{get;set;}
public integer number2{get;set;}
public string operator{get;set;}
public double result{get;set;}
public void calculation()
{
if(operator.contains('+'))
result=number1+number2;
if(operator.contains('-'))
result=number1-number2;
}
}



For further reference, you can check the below,
https://developer.salesforce.com/forums/?id=906F00000009BhFIAU



Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Rakesh M 20Rakesh M 20
Hi Abhishek,
Thanks for Your Respone.
I am using "VF Remoting" to perform this
AbhishekAbhishek (Salesforce Developers) 
Through VF Remoting I haven't tried yet.
Willie DiazWillie Diaz
Wow, this looks fabulous! True, for me so far it looks very difficult, but I am learning. Together with our group, we are working on a project to build a similar calculator for solving geometry assignments. So I practice geometry a lot. By the way, this resource https://plainmath.net/secondary/geometry/high-school-geometry/congruence is the best thing that happened to me (I say it as a newbie student). I would also be grateful if you could recommend good resources for learning math!