2017年4月9日日曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 4.(Arrays - Putting Some Order in Your Data)、How to iterate over an array の CODE MAGNETS(No. 2492) を取り組んでみる。

CODE MAGNETS(No. 2492)

コード(Emacs)

HTML5

<button id="run0">run</button><button id="clear0">clear</button>
<pre id="output0"></pre>

<script src="sample3.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0');

let products = ['Choo Choo Chocolate',
                'Icy Mint',
                'Cake Batter',
                'Bubblegum'],
    hasButtleGum = [false, false, false, true];

let output = () => {
    pre0.textContent += `for loop\n`;
    for (let i = 0, max = hasButtleGum.length; i < max; i += 1) {
        if (hasButtleGum[i]) {
            pre0.textContent += `${products[i]} contains bubble gum\n`;
        }
    }
    pre0.textContent += `forEach 1\n`;
    hasButtleGum.forEach((bln, i) => {
        if (bln) {
            pre0.textContent += `${products[i]} contains bubble gum\n`;
        }
    });
    pre0.textContent += `forEach 2\n`;
    products.forEach((product, i) => {
        if (hasButtleGum[i]) {
            pre0.textContent += `${product} contains bubble gum\n`;
        }
    });
};

btn0.onclick = output;

btn1.onclick = () => {
    pre0.textContent = '';
};

output();



    




 
						

0 コメント:

コメントを投稿

Comments on Google+: