• ASHISH KUMAR YADAV 7
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
DuplicateMKT hold duplicate value like [65,65,5,5] i need to take the unique value and sum that value and put in map-outletIdByTotalmarket thisis map<sting,string> what is the good solution use map or set and do the calculation anyone can help.

//Code Started from here             
   Decimal totalmarket=0.0;
                   Decimal mkttotal=0.0;
                   List<String> DuplicateMKT =new List<String>();
                   List<String> noDupes;
                   for(String outletmktId : lstStagingByOutletId.keySet()){
                   List<SNR_Staging__c> currlist = lstStagingByOutletId.get(outletmktId);
                       for(SNR_Staging__c currrecords : currlist){
                           if(!String.isEmpty(currrecords.SNR_HCV_MARKET_R6M__c) ){ 
                               DuplicateMKT.add(currrecords.SNR_HCV_MARKET_R6M__c);
                               System.debug('DuplicateMKT-->'+DuplicateMKT);
                               noDupes = new List<String>(new Set<String>(DuplicateMKT));
                               System.debug('noDupes-->'+noDupes);
                           }
                       }
                       for(String s: noDupes){
                           totalmarket = totalmarket + Decimal.valueOf(s);
                           System.debug('totalmarket--->'+totalmarket);
                           mkttotal += Decimal.valueOf(s);
                           System.debug('mkttotal--->'+mkttotal);
                       }
                       outletIdByTotalmarket.put(outletmktId,mkttotal);
                       System.debug('outletIdByTotalmarket-->'+outletIdByTotalmarket);
                   }
//code end here

 
Hello Team,

I want to calculate in quiz app ex.suppose user is selection question 1 option (a.) then multiplied by 4,  if they select (b.) then multiplied by 3,
if they select (c.) then multiplied by 2,if they select (d.) then multiplied by 1,based on option selection score will be calculated. and based on score I am display the smiles based on score using static resource. but  I am facing the problem because of I have implemented based on question and correct answer option selection .


below is my code-

lwcquiz.html
=========
<template>
<lightning-card title="BMI Quiz App">
<div class="slds-m-around_medium">
<template if:true={isSubmitted}>
<!-- <div class={isScoredFull}> You Health Score is {correctAnswer} Out of {questions.length} </div> -->
<div class={isScoredFull}> You Health Score is {correctAnswer}</div>
</template>
<template if:true={enablesmile1}>
<img src={GreenImage}  height="30px"
width="35px">
</template>
<template if:true={enablesmile2}>
    <img src={YellowImage}  height="30px"
    width="35px">
</template>
<template if:true={enablesmile3}>
<img src={RedImage}  height="30px"
width="35px">
</template>


<form>
<template for:each={questions} for:item="quiz">
<div key={quiz.id} class="slds-m-bottom_medium">
<div><strong>{quiz.id} :- {quiz.ques}</strong></div>
<div class="slds-grid slds-grid_vertical slds-m-bottom_x-small">
<div class="slds-col">
<input type="radio" name={quiz.id} value="a" onchange={changeHandler}/>
{quiz.answers.a}
</div>
<div class="slds-col">
<input type="radio" name={quiz.id} value="b" onchange={changeHandler}/>
{quiz.answers.b}
</div>
<div class="slds-col">
<input type="radio" name={quiz.id} value="c"  onchange={changeHandler}/>
{quiz.answers.c}
</div>
<div class="slds-col">
<input type="radio" name={quiz.id} value="d" onchange={changeHandler} />
{quiz.answers.d}
</div>
</div>
</div>
</template>
<div class="slds-grid slds-grid_align-center">
<lightning-button variant="brand"
type="submit" label="Submit"
title="Submit Quiz"
class="slds-col slds-m-around_medium"
disabled={allNotSelected}
onclick={submitHandler}>
</lightning-button>
<lightning-button variant="brand"
type="reset" label="Reset"
title="Reset Quiz"
class="slds-col slds-m-around_medium"
onclick={resetHandler}>
</lightning-button>
</div>
</form>
</div>
</lightning-card>
</template>

lwcquiz.js
========

import { LightningElement } from 'lwc';
import Green_Image from '@salesforce/resourceUrl/GreenImage';
import Yellow_Image from '@salesforce/resourceUrl/YellowImage';
import Red_Image from '@salesforce/resourceUrl/RedImage';
export default class BmiQuiz extends LightningElement {
     GreenImage=Green_Image;
     YellowImage=Yellow_Image;
     RedImage=Red_Image;
selected={}
correctAnswer=0
isSubmitted=false
enablesmile1=false;
enablesmile2=false;
enablesmile3=false;
optionvalue;
questions=[
{
    id:'1',
    ques:'I have felt cheerful and in good spirits  ?',
    answers:{
    a:'All of the time.',
    b:'Most of the time.',
    c:'Half of the time.',
    d:'Some of the time.'
},
    correctAnswer:'b'
},
{
    id:'2',
    ques:'I have felt calm and relaxed  ?',
    answers:{
        a:'All of the time.',
        b:'Most of the time.',
        c:'half of the time.',
        d:'Some of the time.'
    },
        correctAnswer:'b'
},
{
    id:'3',
    ques:'I have felt active and vigorous ?',
    answers:{
        a:'All of the time.',
        b:'Most of the time.',
        c:'half of the time.',
        d:'Some of the time.'
    },
        correctAnswer:'c'
},

{
    id:'4',
    ques:'I woke up feeling fresh and rested ?',
    answers:{
        a:'All of the time.',
        b:'Most of the time.',
        c:'Half of the time.',
        d:'Some of the time.'
    },
        correctAnswer:'d'
    },
{
    id:'5',
    ques:' My daily life has been filled with things that interest me ?',
    answers:{
        a:'All of the time.',
        b:'Most of the time.',
        c:'half of the time.',
        d:'Some of the time.'
    },
        correctAnswer:'b'
}]
get allNotSelected(){
    return !(Object.keys(this.selected).length===this.questions.length)
}
get isScoredFull(){
    return `slds-text-heading_large ${this.questions.length===this.correctAnswer?
        'slds-text-color_success':'slds-text-color_error'
    }`
}
changeHandler(event){
console.log("value",event.target.value)
console.log("name",event.target.name)
const{name,value}=event.target
this.selected={...this.selected,[name]:value}
 this.optionvalue=this.questions.filter(item=>this.selected[item.value])
console.log('optionvalue'+this.optionvalue)
}
submitHandler(event){
  event.preventDefault()
  let correct=  this.questions.filter(item=>this.selected[item.id]===item.correctAnswer)
  this.correctAnswer=correct.length
  this.isSubmitted=true
  console.log('correctAnswer',this.correctAnswer)
 
if(this.correctAnswer ==4){
    this.enablesmile1=true;
    this.enablesmile2=false;
    this.enablesmile3=false;
 }
 else if( this.correctAnswer==3 ){
   this.enablesmile2=true;
   this.enablesmile3=false;
   this.enablesmile1=false;
  }
 else if( this.correctAnswer==2 ){
    this.enablesmile3=true;
    this.enablesmile2=false;
    this.enablesmile1=false;

 }
}
resetHandler(){
    this.selected={}
    this.correctAnswer=0
    this.isSubmitted=false
}
}
Hello folks,

I am facing error while implementing bmi in lwc.can anyone help me to convert this html file and js file in lwc itry to convert but getting some error .i am new 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;
    }
}
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!