如何设置它以便有两种不同的价格

时间:2014-12-11 00:26:33

标签: javascript html function

我如何让我的程序运作,以便它为美食比萨饼和普通比萨饼提供单独的价格?然后将它们与计算功能一起添加。

因此,如果我订购1个普通披萨和1个美食,它会给我合适的费用。

普通比萨饼是:夏威夷,奶酪,素食,至尊,意大利辣香肠, 美食比萨是:肉类爱好者,鸡肉,虾。

<html>
<title> Pete's Pizza </title>

<script>


//Menu: Hawaiian, Cheese, Veggie, supreme, pepperoni, meat-lovers, chicken, prawn.

//global variables
var totalNumber = 0;   // for total number of Pizzas ordered
var pizzaPrice = 9.50;   // price of each pizza
var pizzaPriceGourmet = 15.50;  //price for gourmet pizzas
var pizzaDelivery = 5;  //price for pizza delivery
var orderTotalPrice = 0;   // total cost of order


function order()
{
  var pizza = new Array()
  pizza[0] = document.form.hawaiian.value                //allocates type of pizza in array
  pizza[0] = Number(pizza[0])                            //converts to number value
  pizza[1] = document.form.cheese.value                 //allocates type of pizza in array            
  pizza[1] = Number(pizza[1])                            //converts to number value
  pizza[2] = document.form.veggie.value                //allocates type of pizza in array
  pizza[2] = Number(pizza[2])                            //converts to number value
  pizza[3] = document.form.supreme.value                //allocates type of pizza in array
  pizza[3] = Number(pizza[3])                            //converts to number value
  pizza[4] = document.form.pepperoni.value                //allocates type of pizza in array
  pizza[4] = Number(pizza[4])                            //converts to number value
  pizza[5] = document.form.meatlovers.value                //allocates type of pizza in array
  pizza[5] = Number(pizza[5])                            //converts to number value
  pizza[6] = document.form.chicken.value                //allocates type of pizza in array
  pizza[6] = Number(pizza[6])                            //converts to number value
  pizza[7] = document.form.prawn.value                //allocates type of pizza in array
  pizza[7] = Number(pizza[7])                            //converts to number value

  totalNumber = pizza[0] + pizza[1] + pizza[2] + pizza[3] + pizza[4] + pizza[5] + pizza[6] + pizza [7];



 alert("You have ordered:" + "\n" +        
         "Hawaiian Pizza:" + pizza [0] + "\n" +
         "Cheese Pizza:" + pizza [1] + "\n" +
         "Veggie Pizza:" + pizza [2] + "\n" + 
         "Supreme Pizza:" + pizza [3] + "\n" +
         "Pepperoni Pizza:" + pizza [4] + "\n" +
         "Meat-Lovers Pizza:" + pizza [5] + "\n" +
         "Chicken Pizza:" + pizza [6] + "\n" +
         "Prawn Pizza:" + pizza [7]);   


if (totalNumber >12) {          // Limit for total amount of Pizzas ordered
    alert("There is a limit of 12 pizzas per order. Please cancel Order and re-order. Current total is:" +totalNumber);
    } else 
    alert("Total number of burgers ordered:" + totalNumber); //Total amount of pizzas ordered
    calculate()  //Begins calculation function
}


function calculate()    //Function for the cost of the order
{
    orderTotalPrice = (totalNumber*pizzaPrice + totalNumber*pizzaPriceGourmet);  //order total+ amount of pizzas ordered * pizza price
    alert("Total cost of pizzas is: $" + orderTotalPrice.toFixed(2) );  //Display order cost as "$"0.00
}
</script>

1 个答案:

答案 0 :(得分:0)

您需要美食披萨价格吗?

单独设置所有比萨饼的价格。然后从有多少订购X价格获得总数。

然后在结尾添加总计以获得价格

声明函数外部的变量,即使这意味着声明一个空函数

Var pizza1;