Skip to content
Snippets Groups Projects
Commit a812538a authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

adding week2 material

parent 7f9f1fc2
No related branches found
No related tags found
No related merge requests found
Showing
with 266 additions and 0 deletions
# only relevant for Mac systems
.DS_STORE
#ingore npm packages
node_modules
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<!-- For responsive page -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Basic HTML5 Document" />
<meta name="keywords" content="HTML5, Responsive, CMSC389N"/>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<title>HTML TEMPLATE</title>
</head>
<body>
<ol type = "I">
<li>Shakira</li>
<li>JLo</li>
</ol>
</body>
</html>
\ No newline at end of file
class Person{
constructor(name, age){
this.name = name;
this.age = age;
}
toString(){
return "Name is: " + this.name + " age is " +this.age;
}
}
class Student{
constructor(name, gpa){
this.name = name;
this.gpa = gpa;
}
toString(){
return "Name is: " + this.name + " gpa is " +this.gpa;
}
}
module.exports.Person = Person;
module.exports.Stu = Student;
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Who are you?', name => {
console.log(`Hey there ${name}!`);
readline.close();
});
const prompt = require('prompt-sync')({sigint: true});
// Random number from 1 - 10
const numberToGuess = Math.floor(Math.random() * 10) + 1;
// This variable is used to determine if the app should continue prompting the user for input
let foundCorrectNumber = false;
while (!foundCorrectNumber) {
// Get user input
let guess = prompt('Guess a number from 1 to 10: ');
// Convert the string input to a number
guess = Number(guess);
// Compare the guess to the secret answer and let the user know.
if (guess === numberToGuess) {
console.log('Congrats, you got it!');
foundCorrectNumber = true;
} else {
console.log('Sorry, guess again!');
}
}
\ No newline at end of file
var emitter = require('events').EventEmitter;
var em = new emitter();
//Subscribe FirstEvent
em.addListener('FirstEvent', function (data) {
console.log('First subscriber: ' + data);
});
//Subscribe SecondEvent
em.on('SecondEvent', function (data) {
console.log('First subscriber: ' + data);
});
// Raising FirstEvent
em.emit('FirstEvent', 'This is my first Node.js event emitter example.');
// Raising SecondEvent
em.emit('SecondEvent', 'This is my second Node.js event emitter example.');
\ No newline at end of file
const callbackPromise = function(value) {
console.log('callback promise');
}
const p = new Promise(function(resolve) {
console.log('making promise')
//return setTimeout(resolve, 1000);
// What happens if we wrap the resolve callback in setTimeout ?
resolve()
});
console.log('Printing.....');
p.then(callbackPromise).then(()=> console.log('chained promise')).then(()=> console.log('last promise'));
setTimeout(function() {
console.log('When will this print????')
}, 3000);
Promise.resolve()
.then(() => {
console.log('resolved promise 1')
//return setTimeout(function() {
// console.log('When will this print??')
//}, 1000);;
})
.then(() => {
console.log('resolved promise 2')
})
console.log('Finished main task');
//p.then(callbackPromise).then(()=> console.log('chained promise'));
Promise.resolve()
.then(() => {
console.log('resolved promise 3')
//return setTimeout(function() {
// console.log('wickeddddddd')
//}, 1000);;
})
.then(() => {
console.log('resolved promise 4')
})
//const EventEmitter = require('events');
const RaiseEvent = require('./raise');
const myEmitter = new RaiseEvent();
//on
myEmitter.on('Greeting', function(){
console.log("I was just greeted!");
})
myEmitter.on('High Five', function(args){
console.log(args);
})
myEmitter.raise();
const http = require('http');
const server = http.createServer(function(request, response){
if(request.url === '/greeting'){
response.write('Hi');
response.end();
}
} );//1
server.on('connection', function(socket){
console.log("Connected!");
});
server.listen(5500);
\ No newline at end of file
const http = require('http');
const fs = require('fs');
const server = http.createServer(function (req, res) {
if (req.url === '/sample') {
fs.readFile('./SampleWebpage.html', function (err, data) {
if (err) {
console.log(err);
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
res.end();
}
})
}
});
server.listen(3000);
\ No newline at end of file
const modules = require('./add');
let p1 = new modules.Person("Jessica", 35);
let s1 = new modules.Stu("Amy", 3.5);
console.log(p1.toString());
console.log(s1.toString());
\ No newline at end of file
const os = require('os');
console.log(os.totalmem());
console.log(os.freemem());
console.log(os.type());
console.log(os.uptime());
{
"name": "week2",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"ansi-regex": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
"integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg=="
},
"prompt-sync": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.2.0.tgz",
"integrity": "sha512-BuEzzc5zptP5LsgV5MZETjDaKSWfchl5U9Luiu8SKp7iZWD5tZalOxvNcZRwv+d2phNFr8xlbxmFNcRKfJOzJw==",
"requires": {
"strip-ansi": "^5.0.0"
}
},
"strip-ansi": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
"integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
"requires": {
"ansi-regex": "^4.1.0"
}
}
}
}
{
"name": "week2",
"version": "1.0.0",
"description": "",
"main": "commandLineAsync.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"prompt-sync": "^4.2.0"
}
}
const path = require('path');
let p = path.parse('/Users/tesla/teaching/summer2020/cmsc389N/cmsc389nsummer2020-student/lectureCodeExamples/week5/node/pathExample.js');
console.log(p);
\ No newline at end of file
const EventEmitter = require('events');
class RaiseEvent extends EventEmitter{
raise() {
//emit events
this.emit('Greeting');
this.emit('High Five', "Down Low", "too slow");
}
}
module.exports = RaiseEvent;
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment