Trying to build Tax Calculator with JS; need simple feedback

Trying to build Tax Calculator with JS; need simple feedback
0

#1

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.


$("#target4").html("#target4"); $("#target4").text("#target4"); how to use it?
#2

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


#3

Notice his edit to prompt.


#4

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.


#5

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


#6

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


#7

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();

#8

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());

@randelldawson 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.


#9

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)


#10

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.


#11

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


#12

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.


#13

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));

#14

2 posts were split to a new topic: Trying to build Tax Calculator with JS; need simple feedback #2