为什么我的程序不将变量分配给对象,然后从中计算?
Why isn't my program assigning variables to the object, and then calculating from it?
这项作业是接受比萨饼订单并制作一个。比萨组装好后,它应该计算比萨的总成本并吐出总答案。我已经在 JsFiddle.net 上玩了两天了,无法弄清楚为什么它不能通过审讯功能并继续进行计算。我已经在这里和那里输入了几个警报语句,但仍然无法弄清楚发生了什么。我最好的猜测是 'myPizza' 属性没有被分配或者它们没有被其他函数共享。我知道第二种可能性不可能是真的,因为 'myPizza' 是一个具有全局属性的全局对象。
recycle = true;
var ingrediants = ['pepperoni ', 'sausage ', 'avocado ', 'chicken ', 'mushrooms ', 'anchovies ', 'bacon ', 'pineapple ', 'beeswax '];
var toppings=[];
var i = -1;
var myPizza= {
size:"",
toppings:[],
stuffCrust:false,
sodaSide: false
};
var greeting = function () {
wantPizza = prompt("Hello! Welcome to Sean's Pizza Shack! Would you like to order a pizza?(Yes or No?)").toLowerCase();
if (wantPizza === 'yes') {
return true;
} else {
alert("Okay then, Get out of my pizza shop because I have customers waiting");
return false;
}
};
var interrogate = function () {
myPizza.size = prompt("Okay sir/Ma'am, Would you like a Small, Medium, Large, or extra Large?(XL)").toLowerCase();
myPizza.toppings = prompt("Alright! So what would you like on your " + myPizza.size + " pizza?" + " We have " + ingrediants).toLowerCase();
do {
i = i + 1;
myPizza.toppings+= toppings[i] =prompt(ingrediants + " Please type one ingrediant at a time to add to your pizza! or you may enter 'ALL' to add all toppings or press OK without entry to stop adding toppings").toLowerCase();
} while (toppings[i] !== "");
//LOOP TO DECIDE IF NEED TO PUSH ALL INGREDIENTS
for (k = 0; k <toppings.length; k++) {
if (toppings[k] === 'all') {
toppings = [];
toppings.push(ingrediants);
} else {
toppings.length -= 1; // IF NOT ALL INGREDIENTS, SUBTRACT '' FROM ADD INGREDIENTS //LOOP
}
}
alert("So you would like " + myPizza.toppings + " on your pizza!");
alert("Okay so i have a " + myPizza.size + " pizza, with " + myPizza.toppings + "!");
myPizza.stuffCrust = prompt("Do you want your pizza to have extra delicious stuffed cheesy crust?(.00)").toLowerCase();
if(myPizza.stuffCrust==='yes') {
myPizza.stuffCrust=true;
}
myPizza.sodaSide = prompt("Would you like a 2 Liter soda with that for an extra .00?");
if(myPizza.sodaSide=== yes) {
myPizza.sodaSide=true;
}
alert(myPizza.sodaSide);
alert(myPizza.toppings);
alert(myPizza.stuffCrust);
alert(myPizza.toppings.length);
};
var up= {
total:0,
Sm:9.00,
Med:12.00,
Lrg: 15.00,
XL: 18.00,
Top: myPizza.toppings.length*1.00,
Stuff:4.00,
Soda: 2.00,
add: function(input) {
total+=input;
}
};
var calculate= function() {
switch(myPizza.size) {
case 'small': up.add(up.Sm);break;
case 'medium': up.add(up.Med);break;
case 'large': up.add(up.Lrg);break;
case 'XL': up.add(up.XL);break;
}
if(myPizza.toppings.length>0) {
up.add(up.Top);
} if (myPizza.stuffCrust) {
up.add(up.Stuff);
} if (myPizza.sodaSide) {
up.add(up.Soda);
}
alert("Okay, looks like your order comes to "+ up.total);
};
var main= function () {
if (greeting() === true) {
interrogate();
calculate();
}
};
main();
在声明中:
if(myPizza.sodaSide=== yes) {
myPizza.sodaSide=true;
}
您需要将 yes
括在引号中:
if(myPizza.sodaSide=== 'yes') {
myPizza.sodaSide=true;
}
这项作业是接受比萨饼订单并制作一个。比萨组装好后,它应该计算比萨的总成本并吐出总答案。我已经在 JsFiddle.net 上玩了两天了,无法弄清楚为什么它不能通过审讯功能并继续进行计算。我已经在这里和那里输入了几个警报语句,但仍然无法弄清楚发生了什么。我最好的猜测是 'myPizza' 属性没有被分配或者它们没有被其他函数共享。我知道第二种可能性不可能是真的,因为 'myPizza' 是一个具有全局属性的全局对象。
recycle = true;
var ingrediants = ['pepperoni ', 'sausage ', 'avocado ', 'chicken ', 'mushrooms ', 'anchovies ', 'bacon ', 'pineapple ', 'beeswax '];
var toppings=[];
var i = -1;
var myPizza= {
size:"",
toppings:[],
stuffCrust:false,
sodaSide: false
};
var greeting = function () {
wantPizza = prompt("Hello! Welcome to Sean's Pizza Shack! Would you like to order a pizza?(Yes or No?)").toLowerCase();
if (wantPizza === 'yes') {
return true;
} else {
alert("Okay then, Get out of my pizza shop because I have customers waiting");
return false;
}
};
var interrogate = function () {
myPizza.size = prompt("Okay sir/Ma'am, Would you like a Small, Medium, Large, or extra Large?(XL)").toLowerCase();
myPizza.toppings = prompt("Alright! So what would you like on your " + myPizza.size + " pizza?" + " We have " + ingrediants).toLowerCase();
do {
i = i + 1;
myPizza.toppings+= toppings[i] =prompt(ingrediants + " Please type one ingrediant at a time to add to your pizza! or you may enter 'ALL' to add all toppings or press OK without entry to stop adding toppings").toLowerCase();
} while (toppings[i] !== "");
//LOOP TO DECIDE IF NEED TO PUSH ALL INGREDIENTS
for (k = 0; k <toppings.length; k++) {
if (toppings[k] === 'all') {
toppings = [];
toppings.push(ingrediants);
} else {
toppings.length -= 1; // IF NOT ALL INGREDIENTS, SUBTRACT '' FROM ADD INGREDIENTS //LOOP
}
}
alert("So you would like " + myPizza.toppings + " on your pizza!");
alert("Okay so i have a " + myPizza.size + " pizza, with " + myPizza.toppings + "!");
myPizza.stuffCrust = prompt("Do you want your pizza to have extra delicious stuffed cheesy crust?(.00)").toLowerCase();
if(myPizza.stuffCrust==='yes') {
myPizza.stuffCrust=true;
}
myPizza.sodaSide = prompt("Would you like a 2 Liter soda with that for an extra .00?");
if(myPizza.sodaSide=== yes) {
myPizza.sodaSide=true;
}
alert(myPizza.sodaSide);
alert(myPizza.toppings);
alert(myPizza.stuffCrust);
alert(myPizza.toppings.length);
};
var up= {
total:0,
Sm:9.00,
Med:12.00,
Lrg: 15.00,
XL: 18.00,
Top: myPizza.toppings.length*1.00,
Stuff:4.00,
Soda: 2.00,
add: function(input) {
total+=input;
}
};
var calculate= function() {
switch(myPizza.size) {
case 'small': up.add(up.Sm);break;
case 'medium': up.add(up.Med);break;
case 'large': up.add(up.Lrg);break;
case 'XL': up.add(up.XL);break;
}
if(myPizza.toppings.length>0) {
up.add(up.Top);
} if (myPizza.stuffCrust) {
up.add(up.Stuff);
} if (myPizza.sodaSide) {
up.add(up.Soda);
}
alert("Okay, looks like your order comes to "+ up.total);
};
var main= function () {
if (greeting() === true) {
interrogate();
calculate();
}
};
main();
在声明中:
if(myPizza.sodaSide=== yes) {
myPizza.sodaSide=true;
}
您需要将 yes
括在引号中:
if(myPizza.sodaSide=== 'yes') {
myPizza.sodaSide=true;
}