0

This is a class project and I am first year student. I am making a tax program and one function calculates the tax bracket the value falls into and the next function will format the value, concatenating dollar signs, adding decimals with the .toFixed(2) and will add appropriate commas with the .toLocaleString(). I am just trying to get the value to pass through the formatMoney() function so I can continue on with the rest of my coding. The exercise is fairly simple but being new it is difficult. Any help would be appreciated, again I just want the value to pass through the second function so I can progress with the rest of the coding which I can do, j

function calculateTax(money) {

    let initAmount = parseFloat(prompt("How much do you have?"));
    // The different taxes to be charged.
    let tax1 = .20;
    let tax2 = .15;
    let tax3 = .05;

    // Taking the user input and comparing it to which value if has to be calculated with.
    if (initAmount <= 5) {
        money = ((initAmount * tax1) + initAmount);

    } else if (initAmount <= 100) {
        money = ((initAmount * tax2) + initAmount);

    } else if (initAmount >= 100.01) {
        money = ((initAmount * tax3) + initAmount);

    } else if (Number.isNaN(initAmount)) {
        money = ("Invalid");

    }

    return (money);
}

calculateTax();
//This function will take the value of money from the calculateTax function and format as requested from client.

function formatMoney(money) {

    let x = money;

    console.log (x);
}

calculateTax(formatMoney(money));
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103

2 Answers2

1

I believe the code that you are looking for is below. Let me explain. This console.log(calculateTax(100)); line starts the process by calling calculateTax and passing in 100 as the initAmount argument. Then the code runs through your calculateTax function. When it's done calculating the amount, it passes that amount to formatMoney, which makes it pretty (I just made up some code, if you want good formatting, check here).

//Example
console.log(calculateTax(100));

function calculateTax(initAmount) {

  let tax1 = .20;
  let tax2 = .15;
  let tax3 = .05;

  if (initAmount <= 5) { 
    money = ((initAmount * tax1) + initAmount);
  } else if (initAmount <= 100) {
    money = ((initAmount * tax2) + initAmount)
  } else if (initAmount >= 100.01) {
    money = ((initAmount * tax3) + initAmount);
  } else if (Number.isNaN(initAmount)) {
    money = ("Invalid");
  }

  let formattedMoney = formatMoney(money);
  return (formattedMoney);
}


function formatMoney(money) {
  let formattedMoney = '$' + money;
  return formattedMoney; 
}
VSO
  • 11,546
  • 25
  • 99
  • 187
0

You simply have the nested functions reversed. This code:

calculateTax(formatMoney(money));

says to pass the value of money to the formatMoney function and pass its results to the calculateTax function.

You presumably mean the reverse. You want to pass the result of the calculateTax function to the formatMoney one:

formatMoney(calculateTax());

And presumably that will return a string -- even if you're only using console.log for now, so you will want to store the result in a variable:

let displayAmount = formatMoney(calculateTax());

That should solve your immediate problem, but if you're interested, there's two more issues you might want to address:

  • You put the isNaN check at the end. Generally best practice is to put such guards up front.

  • Your calculateTax function declares the parameter money, but you never use it, simply redefining in your if-else blocks. This is also not a great practice. You would be better with local variable. (let number;). Or better, you might put the prompt outside the function and pass in that value to calculateTax. This makes the calculateTax function more testable, and means your code is more modular.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103