You need to sign in to do that
Don't have an account?
WonJeung
Simple calculator using Lightning Components
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!
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!
Change your onClick - > onclick. In your case the controller method was not fire.
Please try below code it works fine for me and let me know if this works for you. If still need modifications do let me know.
Component :
Controller :
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Component:
Controller:
Application:
I hope it helps you.
Kindly 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. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Change your onClick - > onclick. In your case the controller method was not fire.
Please try below code it works fine for me and let me know if this works for you. If still need modifications do let me know.
Component :
Controller :
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
https://sfdctree.wordpress.com/2020/04/12/simple-calculator-using-lwc/
You can refer to this blog to build a calculator app in Aura.
https://viewonreview.com/salesforce-lightning-tutorial-for-beginners/
Hope it helps! :)
Component
_______________________________________________________________________________________________________________
Controller
________________________________________________________________________________________________________________
Helper
I am facing error while implementing bmi in lwc.
code using html and javascript
========================
.html file
=======
<!DOCTYPE html>
<html lang="en">
<head>
<title>BMI Calculator</title>
<!--Google Font-->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500;700&display=swap" rel="stylesheet">
<!--Stylesheet-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<input type="range" min="20" max="200" value="20" id="weight" oninput="calculate()">
<span id="weight-val">20 kg</span>
</div>
<div class="row">
<input type="range" min="100" max="250" value="100" id="height" oninput="calculate()">
<span id="height-val">100 cm</span>
</div>
<p id="result">20.0</p>
<p id="category">Normal weight</p>
</div>
<!--Script-->
<script src="script.js"></script>
</body>
</html>
.js file
======
function calculate(){
var bmi;
var result = document.getElementById("result");
var weight = parseInt(document.getElementById("weight").value);
document.getElementById("weight-val").textContent = weight + " kg";
var height = parseInt(document.getElementById("height").value);
document.getElementById("height-val").textContent = height + " cm";
bmi = (weight / Math.pow( (height/100), 2 )).toFixed(1);
result.textContent = bmi;
if(bmi < 18.5){
category = "Underweight";
result.style.color = "#ffc44d";
}
else if( bmi >= 18.5 && bmi <= 24.9 ){
category = "Normal Weight";
result.style.color = "#0be881";
}
else if( bmi >= 25 && bmi <= 29.9 ){
category = "Overweight";
result.style.color = "#ff884d";
}
else{
category = "Obese";
result.style.color = "#ff5e57";
}
document.getElementById("category").textContent = category;
}
converting above file in lwc
=======================
bmi.html
==========
<template>
<div class="slds-box slds-theme_shade">
<lightning-input type="number" name="height" id="height" label="Height" onchange={handleNumberoneChange} value={firstNumber}></lightning-input>
<lightning-input type="number" name="weight" id="weight" label="Weight" onchange={handleNumberTwoChange} value={secondNumber}></lightning-input>
<b>Body Mass Index [BMI] is : </b>
<!--<P>{resultValue}</p> -->
<lightning-button label="Calculate" onclick={bmi}> </lightning-button>
</div>
</template>
Bmi.js
======
import { LightningElement,track } from 'lwc';
export default class BmiLwc extends LightningElement {
@track firstNumber;
@track secondNumber;
handleNumberoneChange(event) {
this.firstNumber = parseInt(event.target.value);
}
handleNumberTwoChange(event) {
this.secondNumber = parseInt(event.target.value);
}
handlecalculate(event){
var bmi;
var result = document.getElementById("result");
var weight = parseInt(document.getElementById("weight").value);
document.getElementById("weight-val").textContent = weight + " kg";
var height = parseInt(document.getElementById("height").value);
document.getElementById("height-val").textContent = height + " cm";
bmi = (weight / Math.pow( (height/100), 2 )).toFixed(1);
result.textContent = bmi;
if(bmi < 18.5){
category = "Underweight";
result.style.color = "#ffc44d";
}
else if( bmi >= 18.5 && bmi <= 24.9 ){
category = "Normal Weight";
result.style.color = "#0be881";
}
else if( bmi >= 25 && bmi <= 29.9 ){
category = "Overweight";
result.style.color = "#ff884d";
}
else{
category = "Obese";
result.style.color = "#ff5e57";
}
document.getElementById("category").textContent = category;
}
}