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.
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
As @IsaacAbrahamson suggested, plus since you are assigning price and taxRate from within the taxCalculator function your function declaration does not need any parameters.
function taxCalculator() {
}
Also, another option is to put the alert inside the function instead of using the return statement and then just call the function like:
taxCalculator();
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);
alert(priceAfterTax);
}
taxCalculator();
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());
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)
I actually like @IsaacAbrahamson’s better than mine, because it allows you to reuse the function in other ways. By me putting the alert inside the function, it would not be possible to use it in another function that was going to display the information differently without that annoying alert popping up also.
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.
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 ).
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));