2016年1月6日水曜日

開発環境

  • 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: USE + OR JOIN?(No. 1121)を取り組んでみる。

USE + OR JOIN?

コード(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,
    body_part,
    adjective,
    animal,
    str1,
    str2;

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

入出力結果(Terminal)

$ jslint sample3.js

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

0 コメント:

コメントを投稿