-2

I am trying to compute the total cost and using alert to display the total cost when the user hit the Submit button. I don't know how to call a var from other functions. Is there a way to do it? Any help will be appreciated. Thank you!

  <script type = "text/javascript">
function CostCalculate(){
    var TotalCost = AppleCost + OrangeCost + BananaCost;
    alert("Your total cost is $ " + TotalCost);
}
function getQuantity1(){
    var promptNum = prompt("Enter numbers of Apple: ");
    var AppleNum = parseInt(promptNum);
    var AppleCost = AppleNum * 0.59;
}
function getQuantity2(){
    var promptNum = prompt("Enter numbers of Orange: ");
    var OrangeNum = parseInt(promptNum);
    var OrangeCost = OrangeNum * 0.49;      
}
function getQuantity3(){
    var promptNum = prompt("Enter numbers of Banana: ");
    var BananaNum = parseInt(promptNum);
    var BananaCost = BananaNum * 0.39;
}

1 Answers1

0

Helo, I think you should read something about the variables scope in javascript. https://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx

You can't acces a variable that's declarated in another function. What you can do is defining the variable outside of the functions.

Dzhambazov
  • 490
  • 2
  • 11