2016年2月19日金曜日

開発環境

  • 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 6(Conditionals and Loops)、PROGRAMMING CHALLENGES #1: AWESOME ANIMALS、#2: RANDOM STRING GENERATOR、#3: H4CK3R SP34K(No. 1836)を取り組んでみる。

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 animals = ['Cat', 'Fish', 'Lemur', 'Komodo Dragon'],
    i,
    max,
    alphabet = 'abcdefghijklmnopqrstuvwxyz',
    random_string = '',
    random_string_length = 6,
    j,
    input = 'javascript is awesome',
    output = '';

console.log(animals);
for (i = 0, max = animals.length; i < max; i += 1) {
    animals[i] = 'Awesome ' + animals[i];
}
console.log(animals);

i = 0;
max = alphabet.length;
for (i = 0; i < 10; i += 1) {
    j = 0;
    while (j < random_string_length) {
        random_string += alphabet[Math.floor(Math.random() * max)];
        j += 1;
    }
    console.log(i + ': ' + random_string);
    random_string = '';
}

for (i = 0, max = input.length; i < max; i += 1) {
    if (input[i] === 'a') {
        output += '4';
    } else if (input[i] === 'e') {
        output += '3';
    } else if (input[i] === 'i') {
        output += '1';
    } else if (input[i] === 'o') {
        output += 'o';
    } else {
        output += input[i];
    }    
}
console.log(input);
console.log(output);

入出力結果(Terminal)

$ node sample.js
[ 'Cat', 'Fish', 'Lemur', 'Komodo Dragon' ]
[ 'Awesome Cat',
  'Awesome Fish',
  'Awesome Lemur',
  'Awesome Komodo Dragon' ]
0: stpqba
1: zifqec
2: wqaoxv
3: tlogjt
4: mnljjr
5: iwvvmr
6: ozicla
7: jqvqee
8: xlnrst
9: vcvass
javascript is awesome
j4v4scr1pt 1s 4w3som3
$

0 コメント:

コメントを投稿