乘以变量

时间:2016-05-03 22:59:53

标签: javascript variables

您好我很困惑如何将变量设置为该变量乘以它的自我。经过大量的互联网搜索尝试不同的东西,我仍然无法让它工作,即使它感觉它应该工作。这是脚本......

var totalSmalls = (totalSmalls*smallPrice);
var totalMediums = (totalMediums*mediumPrice);
var totalLarges = (totalLarges*largePrice);
var totalNumber = totalsmalls + totalMediums + totalLarges;

这些变量都尽我所能。我还要附上完整的程序......



<!DOCTYPE html>
<html>
<title>PizzaLife</title>
<script>
	
var smallPrice = 3.50;
var mediumPrice = 5.00;
var largePrice = 9.99;
var totalSmalls = 0;
var totalMediums = 0;
var totalLarges = 0;
var totalNumber = 0;

function calculateOrder()//calculate the number of smalls, larges and medium pizzas
{
var pizza = new Array()
var smallPizza = new Array()
smallPizza[0] = Number(document.form.smallP.value)
smallPizza[1] = Number(document.form.smallC.value)
smallPizza[2] = Number(document.form.small4.value)
smallPizza[3] = Number(document.form.smallT.value)
smallPizza[4] = Number(document.form.smallM.value)

var mediumPizza = new Array()
mediumPizza[5] = Number(document.form.mediumP.value)
mediumPizza[6] = Number(document.form.mediumC.value)
mediumPizza[7] = Number(document.form.medium4.value)
mediumPizza[8] = Number(document.form.mediumT.value)
mediumPizza[9] = Number(document.form.mediumM.value)

var largePizza = new Array()
largePizza[10] = Number(document.form.largeP.value)
largePizza[11] = Number(document.form.largeC.value)
largePizza[12] = Number(document.form.large4.value)
largePizza[13] = Number(document.form.largeT.value)
largePizza[14] = Number(document.form.largeM.value)

totalSmalls = smallPizza[0] + smallPizza[1] + smallPizza[2] + smallPizza[3] + smallPizza[4];
totalMediums = mediumPizza[5] + mediumPizza[6] + mediumPizza[7] + mediumPizza[8] + mediumPizza[9];
totalLarges = largePizza[10] + largePizza[11] + largePizza[12] + largePizza[13] + largePizza[14];

alert ("you have ordered " + totalSmalls + " Small pizzas " + totalMediums + " Mediums and " + totalLarges + " Larges." );
var totalSmalls = (totalSmalls*smallPrice);
var totalMediums = (totalMediums*mediumPrice);
var totalLarges = (totalLarges*largePrice);
var totalNumber = totalsmalls + totalMediums + totalLarges;
alert ("Your order will cost a total of $" + totalNumber + "yaaz");
}

function resetOrder() //to reset the page and order
{
if (confirm ("Are you sure you want to reset the order?") == true)
{
window.location.reload()
}
}

</script>
<body>
<p><font face = "jokerman" size="10" color="DarkGreen"><b>PizzaLife  NZ</b></font>
<font face="jokerman" size="6" color="DarkGreen"><i>  Online</i></font></p>
	
<p><font face = "Arial Black" size = "2">Use the arrows to take your pick of our pizzas!</font><br>

<i>note: max per order is 99 pizzas</i></p>
<p>Large Pizza: $9.99 <br>Medium Pizza: $5.00 <br>Small Pizza: $3.50</p>

<form name = "form">
<font face = "Arial Black" size = "2">
<font face = "Arial Black" size = "2" color = "red">Pepperoni Pizza: </font><br>
Larges:<input type = "number" min = "0" max = "99" name = "largeP"> Mediums:<input type = "number" min = "0" max = "99" name = "mediumP"> smalls:<input type = "number" min = "0" max = "99" name = "smallP"><br>
<font face = "Arial Black" size = "2" color = "red">Cheesy Pizza: </font><br>
Larges:<input type = "number" min = "0" max = "99" name = "largeC"> Mediums:<input type = "number" min = "0" max = "99" name = "mediumC"> smalls:<input type = "number" min = "0" max = "99" name = "smallC"><br>
<font face = "Arial Black" size = "2" color = "red">4Meats Pizza: </font><br>
Larges:<input type = "number" min = "0" max = "99" name = "large4"> Mediums:<input type = "number" min = "0" max = "99" name = "medium4"> smalls:<input type = "number" min = "0" max = "99" name = "small4"><br>
<font face = "Arial Black" size = "2" color = "red">TooHot Pizza: </font><br>
Larges:<input type = "number" min = "0" max = "99" name = "largeT"> Mediums:<input type = "number" min = "0" max = "99" name = "mediumT"> smalls:<input type = "number" min = "0" max = "99" name = "smallT"><br>
<font face = "Arial Black" size = "2" color = "red">minceNcheese Pizza: </font><br>
Larges:<input type = "number" min = "0" max = "99" name = "largeM"> Mediums:<input type = "number" min = "0" max = "99" name = "mediumM"> smalls:<input type = "number" min = "0" max = "99" name = "smallM"><br>
</font>
</form>

<p>
---------------------&lsaquo;<input type = "button" value = "reset" onclick = "resetOrder()"><input type = "button" value = "Get Em!" onclick = "calculateOrder()">&rsaquo;------------------------
</p>

</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

从此行中删除var

var totalSmalls = (totalSmalls*smallPrice);

从其他类似的方面来看,得到这个:

totalSmalls = (totalSmalls*smallPrice);
totalMediums = (totalMediums*mediumPrice);
totalLarges = (totalLarges*largePrice);
totalNumber = totalSmalls + totalMediums + totalLarges;

您正在尝试更新在脚本顶部声明的全局变量,但是通过在函数内使用var,您创建了相同名称的局部变量。这实际上不是语法错误,但如果您希望变量是全局变量,则代码将无法正常工作。

导致运行时错误的是在第四行中totalSmalls中有一个小写“s”。

答案 1 :(得分:0)

你的第二个警报不起作用的原因是因为你在这一行中有一个拼写错误:

std::stable_partition

它应该是var totalNumber = totalsmalls + totalMediums + totalLarges;

中的大写字母
totalSmalls

是的,你需要从每个大小的总数中删除var关键字。您正在重新声明它,当浏览器读取您的javascript时,它会用一个全新的变量替换一个变量。