2014年9月12日金曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 10(Liberated functions: First Class Functions)、EXERCISE(p.456)を解いてみる。

EXERCISE(p.456)

コード(BBEdit, Emacs)

var createDrinkOrder = function (passegner) {
        var orderFunction;
        
        if (passegner.ticket === 'firstclass') {
            orderFunction = function () {
                print('Would you like a cocktail or wine?');
            };
        } else if (passegner.ticket === 'premium') {
            orderFunction = function () {
                print('Would you like wine, cola or water?');
            };
        } else {
            orderFunction = function () {
                print('Would you like cola or water?');
            };
        }
        
        return orderFunction
    },
    createDinnerOrderFunction = function (passegner) {
        var orderFunction;
        
        if (passegner.ticket === 'firstclass') {
            orderFunction = function () {
                print('Would you like chicken or pasta?');
            };
        } else if (passegner.ticket === 'premium') {
            orderFunction = function () {
                print('Would you like a snack box or cheese plate?');
            };
        } else {
            orderFunction = function () {
                print('Would you like peanuts or pretzels?');
            };
        }
        
        return orderFunction;
    },
    serveCustomer = function (passegner) {
        var getDrinkOrderFunction = createDrinkOrder(passegner),
            getDinnerOrderFunction = createDinnerOrderFunction(passegner);
        
        getDrinkOrderFunction();

        getDrinkOrderFunction();
        getDinnerOrderFunction();
        getDrinkOrderFunction();
        
        getDrinkOrderFunction();
    },
    servePassengers = function (passengers) {
        var i,
            max;
        for (i = 0, max = passengers.length; i < max; i += 1) {
            print(passengers[i].name + ', ' + passengers[i].ticket);
            serveCustomer(passengers[i]);
        }
    },
    passengers = [ {name: "Jane Doloop", paid: true, ticket: 'firstclass'},
                   {name: "Dr. Evel", paid: true, ticket: 'coach'},
                   {name: "Sue Property", paid: false, ticket: 'premium'},
                   {name: "John Funcall", paid: true, ticket: 'coach' }];
                    
servePassengers(passengers);











						

0 コメント:

コメントを投稿