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
MokshadaMokshada 

BMI calculator using aura component

Hi, I was trying to implement Bmi calculator using aura component but it is not working when i click on button it is showing zero after adding alerts in js i got to know that I am getting value as 0 of height and weight, how to make this code work can anyone help?
apex:
public class BMICalculatorController {
    @AuraEnabled
    public static Decimal calculateBMI(Decimal height, Decimal weight) {
        Decimal heightInMeters = height / 100;
        Decimal bmi = weight / (heightInMeters * heightInMeters);
        System.debug(bmi);
        return bmi;
    }
}

component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="BMICalculatorController">
    <aura:attribute name="height" type="Decimal" default="0"/>
    <aura:attribute name="weight" type="Decimal" default="0"/>
    <aura:attribute name="bmiResult" type="Decimal" default="0"/>
    
    <div>
        <label>Height (in CM):</label>
        <input type="number" aura:id="heightInput" value="{!v.height}"/>
    </div>
    <div>
        <label>Weight (in KG):</label>
        <input type="number" aura:id="weightInput" value="{!v.weight}"/>
    </div>
    <div>
        <button onclick="{!c.calculateBMI}">Calculate BMI</button>
    </div>
    <div>
        <h2>BMI Result: {!v.bmiResult}</h2>
    </div>
</aura:component>

Controller:
({
    calculateBMI: function(component, event, helper) {
        var height = component.get("v.height");
        var weight = component.get("v.weight");
        alert(component.get("v.height"));
        alert(component.get("v.weight"));
        //if (height > 0 && weight > 0) {
            var action = component.get("c.calculateBMI");
            action.setParams({
                height: height,
                weight: weight
            });
            
            action.setCallback(this, function(response) {
                var state = response.getState();
                //alert(response.getState())
                alert(state)
                if (state === "SUCCESS") {
                    var bmiResult = response.getReturnValue();
                    
                    component.set("v.bmiResult", bmiResult);
                } else {
                    alert("Error");
                }
            });
            
            $A.enqueueAction(action);
        
    }
})


Thanks,
 
Arun Kumar 1141Arun Kumar 1141
Hi 
Mokshada,
Check below code it will work:-
Component:-
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="BMICalculatorController">
    <aura:attribute name="height" type="Decimal" default="0"/>
    <aura:attribute name="weight" type="Decimal" default="0"/>
    <aura:attribute name="bmiResult" type="Decimal" default="0"/>
    
    <div>
        <label>Height (in CM):</label>
        <lightning:input aura:id="heightInput" name="heightInput" label="heightInput"  value="{!v.height}"/>
        
    </div>
    <div>
        <label>Weight (in KG):</label>
        <lightning:input aura:id="weightInput" name="weightInput" label="weightInput"  value="{!v.weight}"/>
    </div>
    <div>
        <button onclick="{!c.calculateBMI}">Calculate BMI</button>
    </div>
    <div>
        <h2>BMI Result: {!v.bmiResult}</h2>
    </div>
</aura:component>

Controller:-
({
    calculateBMI: function(component, event, helper) {
        
        var heightInput = component.find("heightInput").get("v.value");
        var weightInput = component.find("weightInput").get("v.value");
    
            var action = component.get("c.calculateBMImethod");
            action.setParams({
                height: heightInput,
                weight: weightInput
            });
            
            action.setCallback(this, function(response) {
                var state = response.getState();

                if (state === "SUCCESS") {
                    var bmiResult = response.getReturnValue();
                    
                    component.set("v.bmiResult", bmiResult);
                } else {
                    alert("Error");
                }
            });
            $A.enqueueAction(action);
    }
})

Apex:-
public class BMICalculatorController {
    @AuraEnabled
    public static Decimal calculateBMImethod(Decimal height, Decimal weight) {
        Decimal heightInMeters = height / 100;
        Decimal bmi = weight / (heightInMeters * heightInMeters);
        System.debug(bmi);
        return bmi;
    }
}

hope this will help you, please mark this as best answer.
Thanks