If/Else for Calculations

Hi,

I’m not sure this is usual but I want to clarify and know some stuffs regarding if/else calculations.

For an example the user have inputted certain amount in the space provided. And then with the data inputted I want to create and if else statement. Like, if they inserted $300 then I want to them know by investing this much they will get 10% more in return. But the rate is not fixed. If the amount is between $300 to $500 then the rate is 10% but if the amount is $600 to $700 then the rate will be 15%.

So how do I create a code that says if they invest this much then they will receive this percentage in return like 5%, 10% and 20%.

I appreciate all the help that could be given to me. Thanks.

I really need help with this. Anyone please help me out here. Thank you.

Greetings,

Javascript has a switch statement for instances where you have more than two possibilities that you have to account for. You could do it with the if/else statement, but it’s cleaner with the multiple possibilities that you have to account for. https://www.w3schools.com/js/js_switch.asp

The logic of what you need is not real clear, so I’m sure you will want to modify this to suit your needs. This is set up to test as the page loads. You will want to put the call to the InterestMessage function in an onChange event of an input box.

$w.onReady(function () {
    let Message = InterestMessage(625);
    console.log(Message);
});
function InterestMessage(amount){
    let ReturnString = "";
    switch (amount) {
        case (amount >= 300 && amount <= 500):
            ReturnString = "Your interest will be 10%";
            break;
        case (amount >= 600 && amount <= 700):
            ReturnString = "Your interest will be 15%";
            break;
        default:
            ReturnString = "Your interest will be 5%";
            break;
    }
    return ReturnString;
}

Hi thank you very much for your help. I have done the coding but still I have few issues. Can you help me see through the code and see which are went wrong. I have a picture attached as well.

$w.onReady( function () {
let Message = InterestMessage(625) ;
console.log(Message);
});
function InterestMessage(amount){
let ReturnString = $w(‘#input2’).value ;
switch (amount) {
case (amount >= 225 && amount <= 274):
ReturnString = 4 ;
break ;
case (amount >= 275 && amount <= 324):
ReturnString = 5 ;
break ;
case (amount >= 325 && amount <= 374):
ReturnString = 6 ;
break ;
case (amount >= 430 && amount <= 479):
ReturnString = 8 ;
break ;
case (amount >= 525 && amount <= 574):
ReturnString = 10 ;
break ;
case (amount >= 625 && amount <= 674):
ReturnString = 12 ;
}
return ReturnString;
}

// as above image i want to make it .OnClick as well. As above image, Your Average Monthly Payback is #input1 and Interest Rate is #input2. So once the user input the amount in #input1 and click “Calculate” which is #button2 the result comes out in #input2.

Please help through this. Thank you and sorry to trouble you.

@testsalesmimota instead of switch (amount) use switch (true).
Check the ranges again (they don’t cover everything).

@jonatandor35 Thank you for the help but I tried the below code. But still not sure where I am doing wrong. Still can’t see the result as wanted.


$w.onReady( function () {
$w(‘#button2’).onClick( function () {
console.log(“button has been pressed”)
$w.onReady( function () {
let Message = IM($w(‘#input1’)) ;
console.log(Message);
});
function IM(amount){
let ReturnString = $w(‘#input2’).value ;
switch ( true ) {
case (amount <= 224):
ReturnString = “invalid” ;
break ;
case (amount >= 225 && amount <= 274):
ReturnString = 4 ;
break ;
case (amount >= 275 && amount <= 324):
ReturnString = 5 ;
break ;
case (amount >= 325 && amount <= 374):
ReturnString = 6 ;
break ;
case (amount >= 375 && amount <= 429):
ReturnString = “invalid” ;
break ;
case (amount >= 430 && amount <= 479):
ReturnString = 8 ;
break ;
case (amount >= 480 && amount <= 524):
ReturnString = “invalid” ;
break ;
case (amount >= 525 && amount <= 574):
ReturnString = 10 ;
break ;
case (amount >= 575 && amount <= 624):
ReturnString = “invalid” ;
break ;
case (amount >= 625 && amount <= 674):
ReturnString = 12 ;
break ;
case (amount >= 675):
ReturnString = “invalid” ;
}
return ReturnString;
}
})
})

@testsalesmimota you messed up the $w.onReady() functions.
But I also don’t understand what is $w(’ #input2 ').value

@jonatandor35 I’m pretty confused too. Let me clarify what I would like to do.

When the average monthly payback is inputted and then the user click calculate I want the interest rate to show depending on the range. If for an example, User entered $240 and if it falls under the range $225 to $274 so the interest rate will be 4%. The Interest rate is #input2, Calculate is #button1 and Your Average Monthly Payback is #input1.

Sorry to trouble you. Thanks again for the help.

@testsalesmimota

Why are you using a user input to display the interest rate and not just a simple textbox.
I thought the user can’t change the rate.
Anyway, let’s assume you need a user input there from some reason, then try:

$w.onReady(function () {
$w('#button1').onClick(function () {
let interest = IM($w('#input1').value);
let interestString = "";
typeof interest === 'number' ? interestString = interest.toString() + '%' : interestString = 'invalid';
    $('#input2').value  = interestString;
    })
});
//and the rest....

@jonatandor35 Thank you very much for the help. Very much appreciated.

Hi @jonatandor35 ,

Can you show the right way to resolve the below issue. Sorry to trouble you again. Thank you :

https://www.wix.com/corvid/forum/community-discussion/helpp-connecting-dataset-in-same-row