Dexter Jin

Experiment07 Updated

1 +// mymodule.js에 정의된 모듈로 불러서 사용하도록 처리
1 +// 아래 2개의 함수를 모듈로 정의하는 것이 필요함
2 +function add(a,b) {
3 + return a+b;
4 +}
5 +function sub(a,b) {
6 + return a-b;
7 +}
1 +// Consumer 코드는 변경할 필요 없음
2 +var Producer = require('./producer');
3 +var producer = new Producer();
4 +
5 +producer.on('add', function(t) {
6 + console.log('add:', t);
7 +});
8 +
9 +producer.start();
1 +var inherits = require('util').inherits;
2 +var EventEmitter = require('events').EventEmitter;
3 +
4 +
5 +var Producer = function () {
6 + if (! (this instanceof Producer)) return new Producer();
7 +
8 + EventEmitter.call(this);
9 +}
10 +
11 +// inherits를 활용, Producer construct가 EventEmitter Construct를 상속받음
12 +inherits(Producer, EventEmitter);
13 +
14 +// producer의 start 함수를 재정의
15 +Producer.prototype.start = function () {
16 +
17 + // Random 스트링을 생성하여 이것을 Event에 실어서 보내야 하는 코드 작성
18 +
19 +
20 +}
21 +
22 +
23 +module.exports = Producer;
1 -var a = 33;
2 -var b = 10;
3 -var c = "Test";
4 -console.log("a + b = ");
5 -result = a + b;
6 -console.log(result);
7 -
8 -console.log("a - b = ");
9 -result = a - b;
10 -console.log(result);
11 -console.log("a / b = ");
12 -result = a / b;
13 -console.log(result);
14 -console.log("a % b = ");
15 -result = a % b;
16 -console.log(result);
17 -console.log("a + b + c = ");
18 -result = a + b + c;
19 -console.log(result);
20 -a = a++;
21 -console.log("a++ = ");
22 -result = a++;
23 -console.log(result);
24 -b = b--;
25 -console.log("b-- = ");
26 -result = b--;
27 -console.log(result);
1 -var a = 33;
2 -var b = 10;
3 -console.log("Value of a => (a = b) => ");
4 -result = (a = b);
5 -console.log(result);
6 -console.log("Value of a => (a += b) => ");
7 -result = (a += b);
8 -console.log(result);
9 -console.log("Value of a => (a -= b) => ");
10 -result = (a -= b);
11 -console.log(result);
12 -console.log("Value of a => (a *= b) => ");
13 -result = (a *= b);
14 -console.log(result);
15 -console.log("Value of a => (a /= b) => ");
16 -result = (a /= b);
17 -console.log(result);
18 -console.log("Value of a => (a %= b) => ");
19 -result = (a %= b);
20 -console.log(result);
1 -var a = 2; // Bit presentation 10
2 -var b = 3; // Bit presentation 11
3 -console.log("(a & b) => ");
4 -result = (a & b);
5 -console.log(result);
6 -
7 -console.log("(a | b) => ");
8 -result = (a | b);
9 -console.log(result);
10 -
11 -console.log("(a ^ b) => ");
12 -result = (a ^ b);
13 -console.log(result);
14 -
15 -console.log("(~b) => ");
16 -result = (~b);
17 -console.log(result);
18 -
19 -console.log("(a << b) => ");
20 -result = (a << b);
21 -console.log(result);
22 -
23 -console.log("(a >> b) => ");
24 -result = (a >> b);
25 -console.log(result);
1 -var a = 10;
2 -var b = 20;
3 -console.log("(a == b) => ");
4 -result = (a == b);
5 -console.log(result);
6 -
7 -console.log("(a < b) => ");
8 -result = (a < b);
9 -console.log(result);
10 -console.log("(a > b) => ");
11 -result = (a > b);
12 -console.log(result);
13 -console.log("(a != b) => ");
14 -result = (a != b);
15 -console.log(result);
16 -console.log("(a >= b) => ");
17 -result = (a >= b);
18 -console.log(result);
19 -console.log("(a <= b) => ");
20 -result = (a <= b);
21 -console.log(result);
1 -var a = 10;
2 -var b = 20;
3 -var linebreak = "<br />";
4 -console.log ("((a > b) ? 100 : 200) => ");
5 -result = (a > b) ? 100 : 200;
6 -console.log(result);
7 -console.log ("((a < b) ? 100 : 200) => ");
8 -
9 -result = (a < b) ? 100 : 200;
10 -console.log(result);
1 +var events = require('events');
2 +var eventEmitter = new events.EventEmitter();
3 +var connectHandler = function connected() {
4 + console.log('connection successful.');
5 + eventEmitter.emit('data_received');
6 +}
7 +
8 +eventEmitter.on('connection', connectHandler);
9 +
10 +eventEmitter.on('data_received', function(){
11 + console.log('data received successfully.');
12 +});
13 +
14 +eventEmitter.emit('connection');
15 +console.log("Program Ended.");
1 +var events = require('events');
2 +var eventEmitter = new events.EventEmitter();
3 +var listner1 = function listner1() {
4 +console.log('listner1 executed.');
5 +}
6 +var listner2 = function listner2() {
7 +console.log('listner2 executed.');
8 +}
9 +
10 +eventEmitter.addListener('connection', listner1);
11 +eventEmitter.on('connection', listner2);
12 +var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
13 +console.log(eventListeners + " Listner(s) listening to connection event");
14 +
15 +eventEmitter.emit('connection');
16 +eventEmitter.removeListener('connection', listner1);
17 +console.log("Listner1 will not listen now.");
18 +
19 +eventEmitter.emit('connection');
20 +eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
21 +console.log(eventListeners + " Listner(s) listening to connection event");
22 +console.log("Program Ended.");
1 -var numbers = new Array(1,2,2,3,4)
2 -for (var num in numbers) {
3 - // key and Value
4 - console.log(num + " = " + numbers[num]);;
5 -}
6 -console.log ("Exiting from the loop!");
1 -var age = 20;
2 -if( age > 18 ) {
3 - console.log("Qualifies for driving");
4 -}
5 -
6 -var age = 15;
7 -if( age > 18 ) {
8 - console.log("Qualifies for driving");
9 -} else {
10 - console.log("Does not qualify for driving");
11 -}
12 -
13 -
14 -var book = "maths";
15 -if( book == "history" ) {
16 - console.log("History Book");
17 -} else if( book == "maths" ) {
18 - console.log("Maths Book");
19 -} else if( book == "economics" ) {
20 - console.log("Economics Book");
21 -} else {
22 - console.log("Unknown Book");
23 -}
1 -var a = true;
2 -var b = false;
3 -console.log("(a && b) => ");
4 -result = (a && b);
5 -console.log(result);
6 -console.log("(a || b) => ");
7 -result = (a || b);
8 -console.log(result);
9 -console.log("!(a && b) => ");
10 -result = (!(a && b));
11 -console.log(result);
1 +
2 +var dt = require('./mymodule');
3 +
4 +console.log(dt.myDateTime());
1 +exports.myDateTime = function () {
2 + return Date();
3 +};
1 -var grade='A';
2 -console.log("Entering switch block");
3 -
4 -switch (grade)
5 -{
6 - case 'A':
7 - console.log("Good job");
8 - break;
9 -
10 - case 'B':
11 - console.log("Pretty good");
12 - break;
13 -
14 - case 'C':
15 - console.log("Passed");
16 - break;
17 -
18 - case 'D':
19 - console.log("Not so good");
20 - break;
21 -
22 - case 'F': console.log("Failed");
23 - break;
24 -
25 - default:
26 - console.log("Unknown grade")
27 -}
28 -
29 -console.log("Exiting switch block");
30 -
31 -
32 -
33 -switch (grade)
34 -{
35 - case 'A':
36 - console.log("Good job");
37 -
38 - case 'B':
39 - console.log("Pretty good");
40 -
41 - case 'C':
42 - console.log("Passed");
43 -
44 - case 'D':
45 - console.log("Not so good");
46 -
47 - case 'F':
48 - console.log("Failed");
49 -
50 - default:
51 - console.log("Unknown grade");
52 -
53 -}
54 -
55 -console.log("Exiting switch block");
1 -var a = 10;
2 -var b = "String";
3 -result = (typeof b == "string" ? "B is String" : "B is Numeric");
4 -console.log("Result => ");
5 -console.log(result);
6 -result = (typeof a == "string" ? "A is String" : "A is Numeric");
7 -console.log("Result => ");
8 -console.log(result);
1 -var name = "Ali";
2 -var money;
3 -money = 2000;
4 -
5 -console.log("my name is " + name);
6 -console.log("my money is " + money);
7 -
8 -var myVar = "global"; // Declare a global variable
9 -function checkscope( ) {
10 - var myVar = "local";
11 - // Declare a local variable
12 - console.log("print myVar " + myVar);
13 -}
14 -
15 -console.log("print myVar " + myVar);
16 -checkscope();
1 -var count = 0;
2 -console.log("Starting Loop ");
3 -
4 -while (count < 10) {
5 - console.log("Current Count : " + count);
6 - count++;
7 -}
8 -console.log("Loop stopped!");
9 -
10 -
11 -var count = 0;
12 -console.log("Starting Loop");
13 -do {
14 - console.log("Current Count : " + count);
15 - count++;
16 -} while (count < 5);
17 -console.log ("Loop stopped!");
1 -function addPrice(amount) {
2 - this.price = amount;
3 -}
4 -function book(title, author) {
5 - this.title = title;
6 - this.author = author;
7 - this.addPrice = addPrice;
8 -}
9 -
10 -var bookList = new Array(new book("Perl1", "Mohtashim"),new book("Smith", "Mohtashim"),new book("Carl", "Mohtashim"),new book("Perl2", "Mohtashim"));
11 -
12 -console.log(bookList);
13 -console.log("After Sorting");
14 -bookList.sort(sortBook);
15 -// sortBook 이라는 comparing function을 완성하시오.
16 -// sortBook(a, b) { if (a > b) return 1; else if (a < b) return -1; else return 0;
17 -// 1 순위 : title
18 -// 2 순위 : author
19 -console.log(bookList);
1 -function slice(str, start, end) {
2 -
3 -}
4 -var str = "Apples are round, and apples are juicy.";
5 -var sliced = str.slice(1, 3);
6 -
7 -// String.slice method와 동일하게 동작하는 slice function을 구현하시오.
8 -// 아래의 console.log 2개의 출력이 동일해야 함
9 -
10 -console.log( sliced );
11 -console.log( slice(str, 1, 3));
1 -function reverse (input) {
2 -
3 -}
4 -var arr = [0, 1, 2, 3];
5 -
6 -// Array.reverse Method와 동일하게 동작하는 reverse function을 구현하시오.
7 -// 아래의 console.log 2개의 출력이 동일해야 함
8 -
9 -console.log ("Reversed array is : " + arr.reverse() );
10 -arr.reverse();
11 -console.log ("Reversed array is : " + reverse(arr));
1 -var test = new Array(1,2,3,4,5,6);
2 -
3 -for (var aa in test) {
4 - console.log(aa);
5 -}