2016年1月4日月曜日

開発環境

  • 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 3(Arrays)、PROGRAMMING CHALLENGES #1: MORE SOPHISTICATED INSULTS(No. 1121)を取り組んでみる。

MORE SOPHISTICATED INSULTS

コード(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 body_parts = ['hand', 'leg', 'head', 'face', 'hip'],
    body_parts_len = body_parts.length,
    adjectives = ['big', 'small', 'beautiful', 'dirty'],
    adjectives_len = adjectives.length,
    animals = ['dog', 'cat', 'lion', 'tiger', 'monkey', 'sheep'],
    animals_len = animals.length,
    i;

for (i = 0; i < 20; i += 1) {
    console.log('Your ' +
                body_parts[Math.floor(Math.random() * body_parts_len)] +
                ' is more ' +
                adjectives[Math.floor(Math.random() * adjectives_len)] +
                ' than a ' + animals[Math.floor(Math.random() * animals_len)] +
                '.');
}

入出力結果(Terminal)

$ jslint sample2.js

sample2.js is OK.
$ node sample2.js
Your hand is more big than a dog.
Your leg is more beautiful than a monkey.
Your hip is more dirty than a lion.
Your hand is more big than a dog.
Your leg is more dirty than a sheep.
Your hip is more small than a dog.
Your hand is more beautiful than a tiger.
Your face is more beautiful than a dog.
Your hand is more dirty than a dog.
Your face is more small than a cat.
Your leg is more beautiful than a dog.
Your face is more dirty than a cat.
Your face is more big than a dog.
Your hip is more small than a cat.
Your leg is more dirty than a monkey.
Your head is more beautiful than a sheep.
Your hip is more small than a lion.
Your hand is more big than a tiger.
Your hand is more dirty than a cat.
Your face is more dirty than a lion.
$ 

0 コメント:

コメントを投稿

Comments on Google+: