diff --git a/lesson1/challenges/array-filtering.js b/lesson1/challenges/array-filtering.js index ec3fcd8..9cbc8cf 100644 --- a/lesson1/challenges/array-filtering.js +++ b/lesson1/challenges/array-filtering.js @@ -24,4 +24,13 @@ The challenge: The function that you pass to the `.filter()` method will should filter out odd numbers leaving only even numbers. Use console.log() to print the filtered array to the terminal. -*/ \ No newline at end of file +*/ + +var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +console.log("this is the array before " + numbers) +var filtered = numbers.filter(function(numbers){ + return (numbers%2===0); +}); + +console.log(filtered); +} diff --git a/lesson1/challenges/function-arguments.js b/lesson1/challenges/function-arguments.js index c88e603..1a95ca3 100644 --- a/lesson1/challenges/function-arguments.js +++ b/lesson1/challenges/function-arguments.js @@ -7,6 +7,17 @@ Within the 'getFullName' function, return the full name according to the country - ex1: `getFullName("Dale", "Seo", "KR")` should return `Seo Dale`. - ex2: `getFullName("Dale", "Seo", "US")` should return `Dale Seo`. -After that, inside the parentheses of console.log(), call the getFullName() function +After that, inside the parentheses of console.log(), call the getFullName() function with your first and last name and country name as the arguments. -*/ \ No newline at end of file +*/ + +function getFullName(firstName, lastName, countryName){ + if(countryName==="KR"){ + return (lastName + " " + firstName); + }else{ + return (firstName + " " + lastName); + } +} + +console.log(getFullName("Ben", "Sadick", "US")); +console.log(getFullName("Ben", "Sadick", "KR")); diff --git a/lesson1/challenges/if-statement.js b/lesson1/challenges/if-statement.js index d98a620..786d99a 100644 --- a/lesson1/challenges/if-statement.js +++ b/lesson1/challenges/if-statement.js @@ -1,7 +1,18 @@ /* Declare a variable named 'fruit'. Make the fruit variable reference the value 'orange' with the type of String. -Then use console.log() to print "The fruit name has more than five characters." +Then use console.log() to print "The fruit name has more than five characters." if the length of the value of fruit is greater than five. Otherwise, print "The fruit name has five characters or less." -*/ \ No newline at end of file +*/ +var fruit = "orange"; + +function checkLength(someString){ + if(someString.length > 5){ + return("String is longer than 5 with a " + someString.length + " length"); + }else{ + return("String is not longer than 5"); + } +} + +console.log(checkLength(fruit)); diff --git a/lesson1/challenges/looping-through-arrays.js b/lesson1/challenges/looping-through-arrays.js index 7d83da1..3f00a39 100644 --- a/lesson1/challenges/looping-through-arrays.js +++ b/lesson1/challenges/looping-through-arrays.js @@ -6,4 +6,10 @@ Define a variable named 'pets' that references this array: Create a for loop that changes each string in the array so that they are plural. After the for loop, use console.log() to print the pets array to the terminal. -*/ \ No newline at end of file +*/ + +var pets = ['cat', 'dog', 'rat']; +for(var i = 0; i < pets.length; i++){ + pets[i]+="s"; +} +console.log(pets); diff --git a/lesson1/challenges/revising-strings.js b/lesson1/challenges/revising-strings.js index 7c1234c..7fa3a9c 100644 --- a/lesson1/challenges/revising-strings.js +++ b/lesson1/challenges/revising-strings.js @@ -18,3 +18,8 @@ The challenge: Use console.log() to print the results of the .replace() method to the terminal. */ + + +var pizza = "pizza is alright"; +pizza = pizza.replace('pizza', 'burger').replace('alright', 'wonderful'); +console.log(pizza); diff --git a/lesson1/challenges/rounding-numbers.js b/lesson1/challenges/rounding-numbers.js index 1ebfad8..109ecc0 100644 --- a/lesson1/challenges/rounding-numbers.js +++ b/lesson1/challenges/rounding-numbers.js @@ -7,14 +7,18 @@ The challenge: Define a variable named 'roundUp' that references the float 1.5. - We will use the Math.round() method to round the number up. + We will use the Math.round() method to round the number up. This method rounds either up or down to the nearest integer. An example of using Math.round(): Math.round(0.5); - Define a second variable named 'rounded' that references the output of the Math.round() method, + Define a second variable named 'rounded' that references the output of the Math.round() method, passing in the roundUp variable as the argument. Use console.log() to print that number to the terminal. -*/ \ No newline at end of file +*/ + +var roundUp = 1.5; +var rounded = Math.round(roundUp); +console.log(rounded); diff --git a/lesson2/challenges/calculator.js b/lesson2/challenges/calculator.js index 43e5b4c..d4ba829 100644 --- a/lesson2/challenges/calculator.js +++ b/lesson2/challenges/calculator.js @@ -4,4 +4,27 @@ Write a command line calulator application - ex2. `$ node calculator.js 5 - 2` should print `3` - ex3. `$ node calculator.js 5 x 3` should print `15` - ex4. `$ node calculator.js 5 / 2` should print `2.5` -*/ \ No newline at end of file +*/ + +//console.log(process.argv); + +var num1 = parseInt(process.argv[2]); +var op = process.argv[3]; +var num2 = parseInt(process.argv[4]); + +switch (op) { + case "+": + console.log(num1 + num2); + break; + case "-": + console.log(num1 - num2); + break; + case "x": + console.log(num1*num2); + break; + case "/": + console.log(num1/num2); + break; + default: + console.log("bad bad operator"); +} diff --git a/lesson2/challenges/counterServer.js b/lesson2/challenges/counterServer.js index 3dc1755..03f0cbc 100644 --- a/lesson2/challenges/counterServer.js +++ b/lesson2/challenges/counterServer.js @@ -2,4 +2,13 @@ Write a HTTP server application which responds with how many times clients have visited. Test: $ curl http://localhost:3000 -*/ \ No newline at end of file +*/ + +var http = require('http'); +var times = 0; +var server = http.createServer(function (req, res){ + res.end('This site has been visited ' + ++times + " times"); + res.end() +}); + +server.listen(3000); diff --git a/lesson2/test.js b/lesson2/test.js new file mode 100644 index 0000000..21f95ed --- /dev/null +++ b/lesson2/test.js @@ -0,0 +1,7 @@ +var http = require('http'); + +var server = http.createServer(function (req, res){ + res.end('Hello world'); +}); + +server.listen(3000); diff --git a/lesson3/challenges/convertBlocking.js b/lesson3/challenges/convertBlocking.js index 21353d0..28f8b5c 100644 --- a/lesson3/challenges/convertBlocking.js +++ b/lesson3/challenges/convertBlocking.js @@ -4,9 +4,16 @@ var fs = require('fs'); var contents; -try { - contents = fs.readFileSync('hello.html'); - console.log(contents.toString()); -} catch (e) { - console.log(e); -} +// try { +// contents = fs.readFileSync('hello.html'); +// console.log(contents.toString()); +// } catch (e) { +// console.log(e); +// } +fs.readFile('hello.html', function(err, contents){ + if(err){ + console.log(err); + }else{ + console.log(contents.toString()); + } +}) diff --git a/lesson3/challenges/copyFile.js b/lesson3/challenges/copyFile.js index c111689..1771577 100644 --- a/lesson3/challenges/copyFile.js +++ b/lesson3/challenges/copyFile.js @@ -1,6 +1,25 @@ // Copy contents from hello.html to hello-copy.html var fs = require('fs'); +let source = 'hello.html'; +let target = 'hello-copy.html'; // 1. Do this synchronously first +// let content = fs.readFileSync(source, 'utf8'); +// console.log(content); +// fs.writeFileSync(target, content, 'utf8'); // 2. Do the same thing asynchronously +fs.readFile(source, 'utf8', (e, data)=> { + if(e){ + console.console.log('Failed to read'); + } else { + console.log(data); + fs.writeFile(target, data, 'utf8', (e)=> { + if(e){ + console.log('Failed to write file'); + } else { + console.log('Done'); + } + }); + } +}); diff --git a/lesson3/challenges/hello-copy.html b/lesson3/challenges/hello-copy.html index e69de29..7804a78 100644 --- a/lesson3/challenges/hello-copy.html +++ b/lesson3/challenges/hello-copy.html @@ -0,0 +1,9 @@ + + + + Hello + + +

Hello, World!

+ + \ No newline at end of file diff --git a/lesson3/demo/data.txt b/lesson3/demo/data.txt new file mode 100644 index 0000000..bb7d237 --- /dev/null +++ b/lesson3/demo/data.txt @@ -0,0 +1 @@ +Hello, This is Dan Rathers Speaking. \ No newline at end of file diff --git a/lesson3/demo/ltcs.txt b/lesson3/demo/ltcs.txt new file mode 100644 index 0000000..0c2d450 --- /dev/null +++ b/lesson3/demo/ltcs.txt @@ -0,0 +1 @@ +Eat,Sleep,Play \ No newline at end of file diff --git a/lesson3/demo/tasks.js b/lesson3/demo/tasks.js new file mode 100644 index 0000000..9421f0f --- /dev/null +++ b/lesson3/demo/tasks.js @@ -0,0 +1,60 @@ +const fs = require('fs'); + +let tasks = ["Eat", "Sleep", "Play"]; +let filename = 'ltcs.txt'; +var command = process.argv[2]; +function readTasks(){ + +} +fs.writeFile(filename, tasks, function(err){ + if(err) console.log(err); +}) +console.log("tasks were written to " + filename); + +console.log("command", command); + +function listTasks(){ + if(tasks.length===0){ + console.log("found no tasks"); + }else{ + console.log(`found ${tasks.length} lists`); + } +} + +function clearTasks(){ + fs.writeFile(filename, '', function(err){ + if(err) console.log(err); + }) +// tasks=[]; + console.log(`Clear up the tasks ( ${tasks.length} )`); + +} + +function addTasks(){ + let task = process.argv.slice(3).join(" "); + console.log('>task :', task); + tasks.push(task); + console.log(`Added the given tasks ( ${tasks.length} )`); + fs.writeFile(filename, tasks, function(err){ + if(err) console.log(err); + }) + console.log("tasks were written to " + filename); +} + +function help(){ + console.log("Type list, clear, or add"); +} + +switch(command){ + case 'list': + listTasks(); + break; + case 'clear': + clearTasks(); + break; + case 'add': + addTasks(); + break; + default: + help(); +} diff --git a/lesson3/demo/writetest.js b/lesson3/demo/writetest.js new file mode 100644 index 0000000..8432a5c --- /dev/null +++ b/lesson3/demo/writetest.js @@ -0,0 +1,29 @@ +const fs = require('fs'); + +let content = "Hello, This is Dan Rathers Speaking." + +fs.writeFile('data.txt', content, function(err){ + if(err){ + console.log(err); + } +}) + +fs.readFile('data.txt', function(err, data){ + if(err){ + console.log(err); + }else{ + console.log(data.toString()); + } +}) + +fs.stat('data.txt', function(err, data){ + if(err){ + console.log(err); + }else{ + console.log(data); + } +}) + + + +console.log("Ran without problem"); diff --git a/lesson4/.DS_Store b/lesson4/.DS_Store new file mode 100644 index 0000000..ba28863 Binary files /dev/null and b/lesson4/.DS_Store differ diff --git a/lesson4/challenges/chatEmitter.js b/lesson4/challenges/chatEmitter.js index f4c8823..a48d451 100644 --- a/lesson4/challenges/chatEmitter.js +++ b/lesson4/challenges/chatEmitter.js @@ -1,13 +1,19 @@ var EventEmitter = require('events'); // 1. Create a new EventEmitter object and assign it to a variable called 'chat'. +let chat = new EventEmitter(); // 2. Create a function named 'logMessage' to log the given 'message' argument to the console using console.log(). - +function logMessage(message){ + console.log(message); +} // 3. Listen for the 'message' event on our new chat object. Remember to add a callback that accepts the message parameter. - +chat.on('message', logMessage); // 4. Add another listener for the 'join' event to log the welcoming message, "Welcome, " + name + "!". - +chat.on('join', (name)=>{ + console.log('Welcome ' + name + '!'); +}) // 5. On the chat object, emit the 'join' event and pass in your name as a string. - -// 6. Finally, Emit the 'message' event twice on the chat object passing in any messages. \ No newline at end of file +chat.emit('message', 'ben'); +// 6. Finally, Emit the 'message' event twice on the chat object passing in any messages. +chat.emit('join', 'dale'); diff --git a/lesson4/challenges/refactorHttpServer.js b/lesson4/challenges/refactorHttpServer.js index b10bebe..00b70a0 100644 --- a/lesson4/challenges/refactorHttpServer.js +++ b/lesson4/challenges/refactorHttpServer.js @@ -1,24 +1,27 @@ var http = require('http'); -var server = http.createServer(function(req, res) { - res.end('Hello, World!'); -}); - -// 1. Add an event listener on the server variable that listens to the 'request' event. +// 1. Add an event listener on the server variable that listens to the 'request' event. // The event listener should take a callback function with two arguments, request and response. +var server = http.createServer(); -// 2. Move the logic for handling the request from the 'http.createServer()' callback to your new 'request' event listener. +// 2. Move the logic for handling the request from the 'http.createServer()' callback to your new 'request' event listener. // Remember to remove the 'http.createServer()' callback once the code has been moved. - +server.on('request', (req, res) =>{ + res.end('hello w0rld'); +}) // 3. Add a second 'request' handler to the HTTP server. - +server.on('request', (req, res) =>{ + console.log('New request coming in...'); +}) // 4. From inside of the new handler, log the message "New request coming in..." using console.log(). - -// 5. Listen for the 'close' event on the server. +server.on('connection', (req, res) =>{ + console.log('A new connection is made!'); +}) +// 5. Listen for the 'close' event on the server. // The event listener should take a callback function that accepts no arguments. // 6. Inside the 'close' callback, log the message "Closing down the server...". server.listen(8080); -console.log('Server running at http://localhost:8080'); \ No newline at end of file +console.log('Server running at http://localhost:8080'); diff --git a/lesson4/demo/.DS_Store b/lesson4/demo/.DS_Store new file mode 100644 index 0000000..042cd69 Binary files /dev/null and b/lesson4/demo/.DS_Store differ diff --git a/lesson4/demo/index.html b/lesson4/demo/index.html new file mode 100644 index 0000000..5f27f1d --- /dev/null +++ b/lesson4/demo/index.html @@ -0,0 +1,18 @@ + + + + + Foobar + + +

Trolololol

+ + + + + diff --git a/lesson4/demo/mockServ.js b/lesson4/demo/mockServ.js new file mode 100644 index 0000000..a221bbf --- /dev/null +++ b/lesson4/demo/mockServ.js @@ -0,0 +1,25 @@ +var http = require('http'); + +var server = http.createServer(); + +server.on('request', function(req, res){ + res.write('
hello there
'); +}); + +server.on('request', function(req, res){ + res.write('hello there1
'); +}); +server.on('request', function(req, res){ + res.write('hello there2
'); +}); + + +server.on('close', function(req, res){ + console.log('bye'); +}) +server.on('request', function(req, res){ + res.end('hello there3'); +}); + + +server.listen(3000); diff --git a/lesson4/demo/script.js b/lesson4/demo/script.js new file mode 100644 index 0000000..c31004e --- /dev/null +++ b/lesson4/demo/script.js @@ -0,0 +1,20 @@ +var EventListener = require('events'); + +var myEmitter = new EventListener(); +let eventNames = myEmitter.eventNames(); +//console.log('event names: ' + myEmitter.eventNames()); + +function hey(){ + console.log('hey'); +} + +myEmitter.on('farewell', (name)=>{ + console.log(name + ' byebye'); +}); +myEmitter.on('greeting', ()=>{ + console.log('yo'); +}); +myEmitter.on('greeting', hey); +//console.log('event names: ', myEmitter.eventNames()); +myEmitter.emit('greeting'); +myEmitter.emit('farewell', 'Ben'); diff --git a/lesson5/challenges/destination.txt b/lesson5/challenges/destination.txt new file mode 100644 index 0000000..e69de29 diff --git a/lesson5/challenges/origin.txt b/lesson5/challenges/origin.txt index 7835ad2..d696580 100644 --- a/lesson5/challenges/origin.txt +++ b/lesson5/challenges/origin.txt @@ -1,11 +1,2 @@ -Apple -Apricot -Banana -Grape -Lemon -Orange -Pineapple -Pomegranate -Quince -Raspberry -Strawberry \ No newline at end of file + +Finished \ No newline at end of file diff --git a/lesson5/challenges/pipeFiles.js b/lesson5/challenges/pipeFiles.js index 3e9912f..a3224d6 100644 --- a/lesson5/challenges/pipeFiles.js +++ b/lesson5/challenges/pipeFiles.js @@ -1,3 +1,16 @@ -// Instead of manually listening for the 'readable' event on the Readable stream, +// Instead of manually listening for the 'readable' event on the Readable stream, // let's use pipe to read from the file, 'origin.txt' and write directly to the file, 'destination.txt'. -var fs = require('fs'); \ No newline at end of file +const fs = require('fs') + +let wStream = fs.createWriteStream('origin.txt') +let rStream = fs.createReadStream('destination.txt') + +// var fs = require('fs'); +// +// var origFile = fs.createReadStream('origin.txt'); +// var destFile = fs.createWriteStream('destination.txt'); + + +/* +I switched these >_< doh! +*/ diff --git a/lesson5/demo/input.txt b/lesson5/demo/input.txt new file mode 100644 index 0000000..11e3af0 --- /dev/null +++ b/lesson5/demo/input.txt @@ -0,0 +1 @@ +stfu world diff --git a/lesson5/demo/output.txt b/lesson5/demo/output.txt new file mode 100644 index 0000000..4d0c76b --- /dev/null +++ b/lesson5/demo/output.txt @@ -0,0 +1 @@ +Hello!stream finished \ No newline at end of file diff --git a/lesson5/demo/server.js b/lesson5/demo/server.js new file mode 100644 index 0000000..9085c00 --- /dev/null +++ b/lesson5/demo/server.js @@ -0,0 +1,11 @@ +const http = require('http') + +http.createServer(function(req, res){ + req.pipe(res) + // req.on('data', (chunk)=> { + // res.write(chunk) + // }) + // req.on('end', () => { + // res.end() + // }) +}).listen(3000) diff --git a/lesson5/demo/test.js b/lesson5/demo/test.js new file mode 100644 index 0000000..11a6d6a --- /dev/null +++ b/lesson5/demo/test.js @@ -0,0 +1,13 @@ +const fs = require('fs') + +let stream = fs.createReadStream('input.txt') +stream.on('data', (chunk) =>{ + console.log(chunk.toString()) +}) +stream.on('end', () => { + console.log('no more data') +}) + +let writeStream = fs.createWriteStream('output.txt') +writeStream.write('Hello!') +writeStream.end('stream finished'); diff --git a/lesson7/challenges/highfive.js b/lesson7/challenges/highfive.js index b24306b..7c10615 100644 --- a/lesson7/challenges/highfive.js +++ b/lesson7/challenges/highfive.js @@ -1,7 +1,9 @@ -// Notice the two different files: highfive.js and highfiveApp.js. +// Notice the two different files: highfive.js and highfiveApp.js. // The code as it's written will not work, highfive.js isn't exporting anything. // Add the proper exports line to have a successful high five! var highfive = function() { console.log("smack!!"); -}; \ No newline at end of file +}; + +module.exports = highfive; diff --git a/lesson7/challenges/highfiveApp.js b/lesson7/challenges/highfiveApp.js index 6e20780..0091a57 100644 --- a/lesson7/challenges/highfiveApp.js +++ b/lesson7/challenges/highfiveApp.js @@ -1,6 +1,6 @@ -// Notice the two different files: highfive.js and highfiveApp.js. +// Notice the two different files: highfive.js and highfiveApp.js. // The code as it's written will not work, highfive.js isn't exporting anything. // Add the proper exports line to have a successful high five! -var highfive = require('./high_five.js'); -highfive(); \ No newline at end of file +var highfive = require('./highfive.js'); +highfive(); diff --git a/lesson7/challenges/logger.js b/lesson7/challenges/logger.js index b9a5853..fa4f95e 100644 --- a/lesson7/challenges/logger.js +++ b/lesson7/challenges/logger.js @@ -1,13 +1,18 @@ -// export the warn, info and error functions so we can use it in logger-app.js by assigning it to the exports object. +// export the warn, info and error functions so we can use it in logger-app.js +// by assigning it to the exports object. +var logger = { + warn: function(message) { + console.log("Warning: " + message); + }, -var warn = function(message) { - console.log("Warning: " + message); -}; + info :function(message) { + console.log("Info: " + message); + }, -var info = function(message) { - console.log("Info: " + message); -}; + error: function(message) { + console.log("Error: " + message); + } +} -var error = function(message) { - console.log("Error: " + message); -}; \ No newline at end of file + +module.exports = logger; diff --git a/lesson7/challenges/loggerApp.js b/lesson7/challenges/loggerApp.js index 4a72914..d51482d 100644 --- a/lesson7/challenges/loggerApp.js +++ b/lesson7/challenges/loggerApp.js @@ -1,6 +1,7 @@ -// export the warn, info and error functions so we can use it in logger-app.js by assigning it to the exports object. +// export the warn, info and error functions so we can use it in logger-app.js +// by assigning it to the exports object. var logger = require('./logger'); logger.info('This is some information'); -logger.warn('something bad is happening'); \ No newline at end of file +logger.warn('something bad is happening'); diff --git a/lesson7/demo/my_module.js b/lesson7/demo/my_module.js new file mode 100644 index 0000000..ee59cfb --- /dev/null +++ b/lesson7/demo/my_module.js @@ -0,0 +1,16 @@ +var canadianDollar = 0.91 + +function roundTwoDecimals(amount) { + return Math.round(amount * 100) / 100; +} + +var canadianToUs = function(canadian) { + return roundTwoDecimals(canadian * canadianDollar); +}; + +var usToCanadian = function(us) { + return roundTwoDecimals(us / canadianDollar); +}; + +exports.canadianToUs = canadianToUs; +exports.usToCanadian = usToCanadian; diff --git a/lesson7/demo/use_my_module.js b/lesson7/demo/use_my_module.js new file mode 100644 index 0000000..e4a0f03 --- /dev/null +++ b/lesson7/demo/use_my_module.js @@ -0,0 +1,8 @@ +var currency = require('./my_module') + + +console.log('50 Canadian dollars equals this amount of US dollars:'); +console.log(currency.canadianToUs(50)); + +console.log('30 US dollars equals this amount of Canadian dollars:'); +console.log(currency.usToCanadian(30));