-1

Why does this not prompt when the page loads? Located in the <body> tags. Want it to prompt when the page loads for purchaseType. Thank you. Goal is for it to continuously prompt for variables and then alert totals, taxes, tip, etc.

    <script>
        var purchaseType = (prompt("Enter purchase type: (Store/Delivery)"));
        var numOfPies;
        var numOfToppings;
        var numofFountainDrinks;
        var deliveryTipOption;
        var deliveryTipAmount;
        var contactlessDeliveryOption;
        var totalPrice;
        var salesTax;
        var tip;
        var finalCost;
        
        if (purchaseType == "Store"){
        numOfPies = Number(prompt("Enter number of pies: "));
        result1 = (numOfPies * 15);
        numOfToppings = Number(prompt("Enter number of toppings: "));
        result2 = (numOfToppings * 0.50);
        numofFountainDrinks = Number(prompt("Enter number of drinks: "));
        result3 = (numofFountainDrinks * 2);
        totalPrice = (result1 + result2 + result3);
        salesTax = (totalPrice * 0.07);
        finalCost = (totalPrice salesTax);
        alert("Total Price: $" totalPrice);
        alert("Sales Tax: $" salesTax.toFixed(2));
        alert("Final Cost: $" finalCost);
        }
        else (purchaseType == "Delivery"){
        numOfPies = Number(prompt("Enter number of pies: "));
        result1 = (numOfPies * 15);
        numOfToppings = Number(prompt("Enter number of toppings: "));
        result2 = (numOfToppings * 0.50);
        deliveryTipOption = (prompt("Enter tip option: (Yes/No)"));
            if (deliveryTipOption == "Yes"){
            deliveryTipAmount = (prompt("Enter tip amount: "));
            contactlessDeliveryOption = (prompt("Enter contactless option: (Yes/No)"));
            }
            else (deliveryTipOption == "No"){
            deliveryTipAmount = 0.00;
            contactlessDeliveryOption = (prompt("Enter contactless option: (Yes/No)"));
            }
        totalPrice = (result1 + result2 + 3.00);
        salesTax = (totalPrice * 0.07);
        finalCost = (totalPrice salesTax);
        alert("Total Price: $" totalPrice);
        alert("Sales Tax: $" salesTax.toFixed(2));
        alert("Tip amount: $" deliveryTipAmount);
        alert("Final Cost: $" finalCost);
        alert("Contactless Delivery: $" contactlessDeliveryOption);
        }
    </script>
  • Your snippet has a syntax error... _"Unexpected identifier 'salesTax'"_ – Phil Oct 21 '22 at 00:01
  • 2
    This line: `finalCost = (totalPrice salesTax);` misses an operator. – Louys Patrice Bessette Oct 21 '22 at 00:01
  • 1
    This line `else (purchaseType == "Delivery"){` misses an `if`... – Louys Patrice Bessette Oct 21 '22 at 00:05
  • 1
    Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). Try using your browser’s [debug capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`), read any errors. – Sebastian Simon Oct 21 '22 at 01:24

1 Answers1

1

This line finalCost = (totalPrice salesTax); misses an operator (which I assumed to be +).
This line else (purchaseType == "Delivery"){ misses an if.
This line alert("Total Price: $" totalPrice); and all other concatenations are missing a +.

Initialize your variables!
Numbers with zeros and strings with their default value.
This will avoid some NaN when a variable gets used without a value.
Also, I suggest you move your calculations and alerts out of the prompting logic.

I added an else in case the purchaseType value is wrong.
I also added an if to avoid showing all zero results.

<script>
  let purchaseType = (prompt("Enter purchase type: (Store/Delivery)"));
  let numOfPies = 0;
  let numOfToppings = 0;
  let numofFountainDrinks = 0;
  let deliveryTipOption = "No";
  let deliveryTipAmount = 0;
  let contactlessDeliveryOption = "No";
  let totalPrice = 0;
  let salesTax = 0;
  let tip = 0;
  let finalCost = 0;
  let result1 = 0;
  let result2 = 0;
  let result3 = 3;

  if (purchaseType === "Store") {
    numOfPies = Number(prompt("Enter number of pies: "));
    numOfToppings = Number(prompt("Enter number of toppings: "));
    numofFountainDrinks = Number(prompt("Enter number of drinks: "));

  } else if (purchaseType === "Delivery") {
    numOfPies = Number(prompt("Enter number of pies: "));
    numOfToppings = Number(prompt("Enter number of toppings: "));
    deliveryTipOption = (prompt("Enter tip option: (Yes/No)"));

    if (deliveryTipOption === "Yes") {
      deliveryTipAmount = (prompt("Enter tip amount: "));
      contactlessDeliveryOption = (prompt("Enter contactless option: (Yes/No)"));
    } else if (deliveryTipOption === "No") {
      contactlessDeliveryOption = (prompt("Enter contactless option: (Yes/No)"));
    }
  } else {
    alert("Wrong input")
  }

  // Calculations
  result1 = (numOfPies * 15);
  result2 = (numOfToppings * 0.50);
  result3 = (numofFountainDrinks * 2);
  totalPrice = (result1 + result2 + result3);
  salesTax = (totalPrice * 0.07);
  finalCost = (totalPrice + salesTax);

  // Results
  if (totalPrice !== 0) {
    alert("Total Price: $" + totalPrice.toFixed(2));
    alert("Sales Tax: $" + salesTax.toFixed(2));
    alert("Tip amount: $" + deliveryTipAmount.toFixed(2));
    alert("Final Cost: $" + finalCost.toFixed(2));
    alert("Contactless Delivery: $" + contactlessDeliveryOption.toFixed(2));
  }
</script>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64