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.
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.
// 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.
@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.
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: