为什么我的程序没有为对象分配变量,然后从中进行计算?

时间:2015-03-05 07:06:32

标签: javascript function oop variables

这项任务是采取披萨订单并建立一个。比萨饼组装好后,应该计算比萨饼的总成本并吐出总答案。我已经在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?($4.00)").toLowerCase();
    if(myPizza.stuffCrust==='yes') {
        myPizza.stuffCrust=true;
    }
    myPizza.sodaSide = prompt("Would you like a 2 Liter soda with that for an extra $2.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();

1 个答案:

答案 0 :(得分:1)

声明中:

if(myPizza.sodaSide=== yes) {
    myPizza.sodaSide=true;
}

您需要在引号中加yes

if(myPizza.sodaSide=== 'yes') {
    myPizza.sodaSide=true;
}