-3

i need help with a javascript code that's calculate the profit of investment with a rate of 4% daily for a period of 30 days : Screenshot 1

var minimumAmount = 5;

function DoCalculate() {
  var temp1 = $("#txtAmount").val(),
    rate = 0.0;

  if (!$("#txtAmount").val()) temp1 = 0;

  var initial = parseFloat(temp1);

  if (initial < minimumAmount) rate = 0;
  else rate = 0.04;

  var totalReturn = parseFloat(initial * rate * 30);

  if (initial < minimumAmount) {
    $("#lblDailyProfit").html("0.00");
    $("#lblHourlyProfit").html("0.00");
    $("#lblTotalProfit").html("0.00");
    $("#lblTotalReturn").html("0.00");
  } else {
    $("#lblDailyProfit").html(parseFloat(initial * rate * 24).toFixed(2));
    $("#lblHourlyProfit").html(parseFloat(initial * rate).toFixed(2));
    $("#lblTotalReturn").html(totalReturn.toFixed(2));
    $("#lblTotalProfit").html((totalReturn - initial).toFixed(2));
  }
}

$(function() {
  DoCalculate();

  $("#txtAmount").blur(function() {
    if (parseFloat($(this).val()) < minimumAmount) $("#txtAmount").val(minimumAmount);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I want to add multiple rates like :

if ( initial >= 3000 && initial <= 4900 ) rate = 0.02;
if ( initial >= 5000 && initial <= 49999 ) rate = 0.04;
if ( initial >= 50000 && initial <= 49999 ) rate = 0.09;

I've tried many times but with not results, I will be happy if someone helps me; Thanks in advance.

Reyno
  • 6,119
  • 18
  • 27
Joseph
  • 1
  • Welcome to Stack Overflow! In what *specific* way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 11 '22 at 11:37
  • Can you please include the HTML for a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Reyno Aug 11 '22 at 11:37
  • @Reyno thanks again [link](https://jsfiddle.net/hy743pov/) As i mentioned i want to make the rate change when the investment amount changes. Thanks. – Joseph Aug 11 '22 at 12:29
  • @Reyno thanks again [link](https://jsfiddle.net/hy743pov/) As i mentioned i want to make the rate change when the investment amount changes. Thanks. – Joseph Aug 11 '22 at 12:31
  • @David Here is a reproducible example of my code : https://jsfiddle.net/hy743pov/ – Joseph Aug 11 '22 at 14:14
  • @David i want to add mupltiple rates depends on the amount entered if ( initial >= 3000 && initial <= 4900 ) rate = 0.02; if ( initial >= 5000 && initial <= 49999 ) rate = 0.04; if ( initial >= 50000 && initial <= 49999 ) rate = 0.09; – Joseph Aug 11 '22 at 14:15
  • @Joseph: "I want" is not a question or a problem. What have you *tried*? What isn't working as expected? When you debug, which specific operation fails? – David Aug 11 '22 at 14:25
  • @David I've tried ` if (initial < minimumAmount) rate = 0; if ( initial >= 3000 && initial <= 4900 ) rate = 0.02; if ( initial >= 5000 && initial <= 49999 ) rate = 0.04; if ( initial >= 50000 && initial <= 49999 ) rate = 0.09;` You can see it here : https://jsfiddle.net/0hLkpndz/ The problem i have , is for the ` if ( initial >= 3000 && initial <= 4900 ) rate = 0.02; ` It shows a negative number. Thanks again. – Joseph Aug 11 '22 at 14:37
  • @Joseph: What specifically "shows a negative number"? If you've narrowed the problem to exactly that one `if` statement, what specifically is failing in that statement? If you're not using a step debugger to examine your code, now is the time to start doing that. The code you claim is the problem does exactly two things, it (1) compares the `initial` variable with hard-coded values and (2) sets the `rate` variable to a hard-coded value. When you debug, what value does `initial` have and what are you expecting the result of the comparison to be? Be specific about your debugging. – David Aug 11 '22 at 14:53
  • @David Firstable thanks again for your help i appreciate it, can you run the code here : https://jsfiddle.net/0hLkpndz/1/ Try to put a number bellow the minimumAmount 3000, it will shows 0 so it's Okay. But the other if & else doesn't work. – Joseph Aug 11 '22 at 15:04
  • if (initial < minimumAmount) rate = 0.0; else if ( initial >= 3000 && initial <= 4900 ) rate = 0.02; if ( initial >= 5000 && initial <= 49999 ) rate = 0.04; else if ( initial >= 50000 && initial <= 500000 ) rate = 0.09; – Joseph Aug 11 '22 at 15:04
  • @Joseph: *"But the other if & else doesn't work."* - Please consult the linked duplicate for more information on what a debugger is and how it can help you. "The if doesn't work" is a non-starter. The one thing we can guarantee is that your code **does** "work" in the sense that it does exactly what it's written to do. Your next step is to use a debugger and step through the code to observe its behavior so you can identify which specific operation doesn't do what you expect. What were the values used? What was the result? What was expected? Why? – David Aug 11 '22 at 15:15

1 Answers1

-1
temp = (amount, days)=>{
    return amount*1.04**days;
}