2017年5月13日土曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 10.(Liberated functions - First Class Functions)の EXERCISE(No. 6745)を取り組んでみる。

EXERCISE(No. 6745)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample4.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];
        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };

let passengers = [
    {name: 'Jane Doloop', paid: true, ticket: 'coach'},
    {name: 'Dr. Evel', paid: true, ticket: 'firstclass'},
    {name: 'Sue Property', paid: false, ticket: 'firstclass'},
    {name: 'John Funcall', paid: true, ticket: 'premiumeconomy'}
];

let createDrinkOrder = (passenger) => {
    let orderFunction;

    if (passenger.ticket === 'firstclass') {
        orderFunction = () => p('Would you like a cocktail or wine');
    } else if (passenger.ticket === 'premiumeconomy') {
        orderFunction = () => p('Would you like wine, cola or water');
    } else {
        orderFunction = () => p('Your choice is cola or water.');
    }
    return orderFunction;
};
let createDinnerOrder = (passenger) => {
    let orderFunction;

    if (passenger.ticket === 'firstclass') {
        orderFunction = () => p('Would you like chicken or pasta');
    } else if (passenger.ticket === 'premiumeconomy') {
        orderFunction = () => p('Would you like a snack box or cheese plate');
    } else {
        orderFunction = () => p('Your choice is peanuts or pretzels.');
    }
    return orderFunction;
};

let serveCustomer = (passenger) => {
    let getDrinkOrderFunction = createDrinkOrder(passenger),
        getDinnerOrderFunction = createDinnerOrder(passenger);
    
    getDrinkOrderFunction();
    getDinnerOrderFunction();
    getDrinkOrderFunction();
};

let output = () => {
    passengers.forEach((passenger) => {
        p(`${passenger.name}, ${passenger.ticket}`);
        serveCustomer(passenger);
    });
};

let clear = () => pre0.textContent = '';

btn0.onclick = output;
btn1.onclick = clear;

output();





    







						

0 コメント:

コメントを投稿

Comments on Google+: