• Mithun S Naik
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hello, I'm new to Lightning Components and I decided to create a calculator using Lightning Components.
Given two numbers, when I click "Add" the result should be displayed.

Here's the code:

calculator.cmp
<aura:component >
    <aura:attribute name = 'num1' type = 'Integer' default = '15'></aura:attribute>
    <aura:attribute name = 'num2' type = 'Integer' default = '20'></aura:attribute>
    <aura:attribute name = 'sum' type = 'Integer'></aura:attribute>
    <div>
        <p>Add</p><lightning:button label = 'Add' onClick = '{!c.add}'/>
        <p>{!v.num1} + {!v.num2} = {!v.sum}</p>
    </div>
</aura:component>

calculatorController.js
({
    add : function(component, event, helper) {     
        //Add numbers
        var num1 = component.get('v.num1');
        var num2 = component.get('v.num2');
        var sumResult = num1 + num2;
        component.set('v.sum', sumResult);
        
    }
})

The add result doesn't show up when I click the button. It'd be great if someone helps! Thanks!

Hi All,

I am trying to build a Simpale calculator in different way. I have created a custome object Calculator__C having 3 fields Var1, Var2, Result.

Bellow is the controller for same but its not working. Please suggest me what needs be modified in bellow code.

public class CalculatorController {
    public Calculator__C  Calc  {get; set;}
   
Public void Add(){
      Calc.Result__c = Calc.Var1__c + Calc.Var2__c; 
        }
Public void Sub(){
      Calc.Result__c = Calc.Var1__c - Calc.Var2__c; 
    }  
Public void Div(){
     Calc.Result__c = Calc.Var1__c / Calc.Var2__c; 
    }
   
}