Dexter Jin

Experiment07 Updated

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