-1

recently started to study Javascript and having an issue with adding two variables together

function btn_click()
{
    var money = parseInt(document.getElementById("money").value);
    var dollar = parseInt(3.67 * money);
    var xfer = parseInt(0);
    var total = parseFloat(dollar + xfer);
    var dollar = dollar.toFixed(2);
    if (isNaN(money))
    {
        alert("Please enter a valid amount!");
    }
    else
{
    if (money > 15000)
    {
    xfer = 0;
    document.writeln("Amount in dirhams: " + dollar +" AED<br>");
    document.writeln("Transfer charges: " + xfer +" AED<br>");
    document.writeln("Total: <span style='font-weight:bold'>" +total+" AED<br>");
    }
    else
    {
    xfer = 25;
    document.writeln("Amount in dirhams: " + dollar +" AED<br>");
    document.writeln("Transfer charges: " + xfer +" AED<br>");
    document.writeln("<span style='font-weight:bold'> Total: "+ total + " AED<br></span>");
    }
}

}

basically, my last line in the "ELSE" sections

document.writeln("Total: <span style='font-weight:bold'>" +total+" AED<br>");

does not work, when I click on my button, everything gets calculated but the last line, the last line will only display my variable "dollar". Lets say for example I input 100 and clicked the button, my first line would be 367.00 AED and my second line would be 25 AED but my last line would say 367 AED, it did not sum the first two lines.

Sorry if I seem unclear, if you need more information i'd be happy to give it

Thank you guys

afzalex
  • 8,598
  • 2
  • 34
  • 61
user3694341
  • 11
  • 1
  • 5

2 Answers2

1

Your problem is in one of the first lines in your script:

var total = parseFloat(dollar + xfer);

You're calculating total before xfer has a meaningful value.
You'll need to calculate total before your document.writeln("Total: ... lines.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • quite clearly xfer isn't something he is reading the value of from the document, instead assigning his own value based on conditions. – Abdul Jabbar Oct 14 '14 at 12:28
  • That's an assumption. As such, it's not a valid reason to downvote a answer. – Cerbrus Oct 14 '14 at 12:29
  • You should first clear out any assumptions in the comments section before posting an answer. Answer means, do this and it will solve your problem! This clearly don't. – Abdul Jabbar Oct 14 '14 at 12:30
  • 1
    No. Answer on SO aren't at all meant to be copy-pasteable solutions to the OP's problem. SO is meant to help people __identify__ and solve their problems. That way, everyone can learn from the OP's problem, and possibly solve similar issues on their own. – Cerbrus Oct 14 '14 at 12:32
  • What's wrong with copy pasteable solutions if they're identifying and solving OP's problem? I first explained what was the problem and then gave complete code which should fix it. – Abdul Jabbar Oct 14 '14 at 12:39
  • 3
    Copy-pasteable solutions promote lazy programming. They're a security risk, and don't teach the programmer anything. All of this because people don't check or try to understand copied code that "just works". – Cerbrus Oct 14 '14 at 12:42
  • I think that's why i first explained the problem and then verbally told him how to fix it before making those changes in his code.. have you even read my answer? – Abdul Jabbar Oct 14 '14 at 12:43
  • @AbdulJabbar doesn't mean that stopping after that "told him how to fix it" makes an answer any less useful – John Dvorak Oct 14 '14 at 12:45
-2

This is because you're assigning only dollar value to the total. What you're doing on the declaration of total is:

var total = parseFloat(parseInt(3.67*money) + parseInt(0)) => parseFloat(3.67*money) //because xfer is 0 at that point

So what you need to do is calculate total after assigning xfer some value:

function btn_click()
{
    var money = parseInt(document.getElementById("money").value);
    var dollar = parseInt(3.67 * money);
    var xfer = parseInt(0);
    var total = 0;
    var dollar = dollar.toFixed(2);
    if (isNaN(money))
    {
        alert("Please enter a valid amount!");
    }
    else
    {
        if (money > 15000)
        {
            xfer = 0;
            document.writeln("Amount in dirhams: " + dollar +" AED<br>");
            document.writeln("Transfer charges: " + xfer +" AED<br>");
            total = parseFloat(Number(dollar) + Number(xfer));
            document.writeln("Total: <span style='font-weight:bold'>" +total+" AED<br>");
        }
        else
        {
            xfer = 25;
            document.writeln("Amount in dirhams: " + dollar +" AED<br>");
            document.writeln("Transfer charges: " + xfer +" AED<br>");
            total = parseFloat(Number(dollar) + Number(xfer));
            document.writeln("<span style='font-weight:bold'> Total: "+ total + " AED<br></span>");
        }
    }

}
Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43