Trying to build Tax Calculator with JS; need simple feedback

The program in question should simply take two inputs, convert each one using Number, and then multiply them – assigning the result to a seperate variable. It should then display that value using an alert() method.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<script>
function taxCalculator(price, taxRate) {
	var price = parseInt(prompt("What is the price of the item you're about to buy? ex: 10.00"));
  var taxRate = parseInt(prompt("What is the state tax rate? ex: 0.08"));
  priceAfterTax = price + (price * taxRate);
  return priceAfterTax
}

alert(priceAfterTax);
</script>
    
</body>
    
</html>

What am I totally missing here? I’ve googled individual facets of this code, and can’t understand why it’s not working in toto.

Is that pseudocode? I am not familiar with this syntax.
Also, do you call the function you define? Maybe use an IIFE.

1 Like

Notice his edit to prompt.

1 Like

the Number() function is built into ES6, and is recommended in use-cases where the specificity of parseInt() is not needed.

In theory, either Number or parseInt() should work just fine.

You are calling alert for priceAfterTax after tax, but you never call the function that returns priceAfterTax tax.
You will want to do something like alert(yourFunction)


I think he was referring to your input typo instead of prompt

I’m having a hard time understanding this. Can you idiot-mode me and just sort of show me the example?

1 Like

Sure.

function taxCalculator() {
  var price = parseInt(prompt("What is the price of the item you're about to buy? ex: 10.00"));
  var taxRate = parseInt(prompt("What is the state tax rate? ex: 0.08"));
  priceAfterTax = price + (price * taxRate);
  return priceAfterTax
}

alert(taxCalculator());

@camperextraordinaire beat me to the parameters :smile:

Hope the example helps. I can explain more if you don’t understand.

Randell’s second way of doing it is great too, it all depends on how you want to approach the problem.

1 Like

My brain just exploded, but I am trying to cope and internalize the above examples.

Thanks so much for the input, and don’t mind me while I figure out what I need to figure out (pretty sure I’ve gotten all the help I NEED, now my brain is just lagging)

1 Like

Good observation, I didn’t even think of that.

1 Like

Thanks for the help guys. This will surely help me gain a more solid understanding of how to declare functions.

Right now, I’m about to hop in the car and head to a meetup on Git! Again, I appreciate the help and will be going back tonight and hammering ‘why yours worked and why mine didn’t’ into my brain.

1 Like

Glad to help! You were originally trying to use parameters. Let me throw another example that you can think about (eventually, once you recover from the first two :stuck_out_tongue_winking_eye:).

var price = parseInt(prompt("What is the price of the item you're about to buy? ex: 10.00"));
var taxRate = parseInt(prompt("What is the state tax rate? ex: 0.08"));

function taxCalculator(price, taxRate) {  
  priceAfterTax = price + (price * taxRate);
  return priceAfterTax
}

alert(taxCalculator(price, taxRate));
1 Like