2017年4月5日水曜日

開発環境

Head First HTML5 Programming (Elisabeth Robson (著)、Eric Freeman (著)、O'Reilly Media)の Chapter 2.(Introducing JavaScript and the DOM: A Little Code)、Make decisions with JavaScript の CODE MAGNETS(No. 1450) を取り組んでみる。

CODE MAGNETS(No. 1450)

コード(Emacs)

HTML5

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

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

JavaScript

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

let word1 = 'a',
    word2 = 'nam',
    word3 = 'nal p',
    word4 = 'lan a c',
    word5 = 'a man a p';

let phrase = '';

for (let i = 0; i < 4; i += 1) {
    if (i === 0) {
        phrase = word5;
    } else if (i === 1) {
        phrase = phrase + word4;
    } else if (i === 2) {
        phrase = phrase + word1 + word3;
    } else if (i === 3) {
        phrase = phrase + word1 + word2  + word1;
    }
}    

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










						

0 コメント:

コメントを投稿