2016年3月31日木曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • JavaScript (プログラミング言語)
  • Node.js(V8) (JavaScript engine)

Javascript for Kids (Nick Morgan 著、Angus Croll 寄稿、Miran Lipovaca イラスト、No Starch Press)のPart 1(Fundamentals)、Chapter 8(Functions)、PROGRAMMING CHALLENGES #1: (DOING ARITHEMETIC WITH FUNCTIONS)、#2: (ARE THESE ARRAYS THE SAME?)、#3: (HANGMAN, USING FUNCTIONS)(No. 2467)を取り組んでみる。

PROGRAMMING CHALLENGES

コード(Emacs)

/*jslint         node    : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/
var add,
    multiply,
    areArraysSame,
    pickWord;

console.log('1.');
add = function (a, b) {
    return a + b;
};
multiply = function (a, b) {
    return a * b;
};

console.log(add(multiply(35325, 9824), 777));

console.log('2.');
areArraysSame = function (a, b) {
    var a_len = a.length,
        b_len = b.length,
        i;

    if (a_len !== b_len) {
        return false;
    }
    for (i = 0; i < a_len; i += 1) {
        if (a[i] !== b[i]) {
            return false;
        }
    }
    return true;
};
console.log(areArraysSame([1, 2, 3], [4, 5, 6]));
console.log(areArraysSame([1, 2, 3], [1, 2, 3]));
console.log(areArraysSame([1, 2, 3], [1, 2, 3, 4]));

入出力結果(Terminal)

$ jslint sample.js

sample.js is OK.
$ node sample.js
1.
347033577
2.
false
true
false
$

コード(Emacs)

/*jslint         node    : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/
/*global document */
document.querySelector('#hangman').onclick = function () {
    var word,
        answerArray = [],
        remainingLetters,
        pickWord,
        setupAnswerArray,
        showPlayerProgress,
        getGuess,
        updateGameState,
        showAnswerAndCongratulaterPlayer,
        guess,
        correctGuesses;

    pickWord = function () {
        var words = ['javascript', 'monkey', 'amazing', 'pancake'];

        return words[Math.floor(Math.random() * 4)];
    };
    setupAnswerArray = function (word) {
        var i,
            max;

        for (i = 0, max = word.length; i < max; i += 1) {
            answerArray[i] = '_';
        }
        return answerArray;        
    };
    showPlayerProgress = function (answerArray) {
        alert(answerArray.join(' '));
    };
    getGuess = function () {
        return prompt('guess a letter, or click Cancel to stop playing.');
    };
    updateGameState = function (guess, word, answerArray) {
        var j,
            max,
            correct = 0;

        for (j = 0, max = word.length; j < max; j += 1) {
            if (word[j] === guess) {
                answerArray[j] = guess;
                correct += 1;
            }
        }
        return correct;
    };
    showAnswerAndCongratulaterPlayer = function (answerArray) {
        alert(answerArray.join(' '));
        alert('Good job! The answer was ' + word);
    };
    word = pickWord();
    answerArray = setupAnswerArray(word);
    remainingLetters = word.length;

    while (remainingLetters > 0) {        
        showPlayerProgress(answerArray);
        guess = getGuess();
        if (guess === '') {
            break;
        }
        if (guess.length !== 1) {
            alert('Please enter a single letter.');
        } else {            
            correctGuesses = updateGameState(guess, word, answerArray);
            remainingLetters -= correctGuesses;
        }
    }
    showAnswerAndCongratulaterPlayer(answerArray);
};

0 コメント:

コメントを投稿

Comments on Google+: