Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a bunch of stuff >_< #31

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lesson1/challenges/array-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
*/

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);
}
15 changes: 13 additions & 2 deletions lesson1/challenges/function-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
*/

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"));
15 changes: 13 additions & 2 deletions lesson1/challenges/if-statement.js
Original file line number Diff line number Diff line change
@@ -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."
*/
*/
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));
8 changes: 7 additions & 1 deletion lesson1/challenges/looping-through-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
*/

var pets = ['cat', 'dog', 'rat'];
for(var i = 0; i < pets.length; i++){
pets[i]+="s";
}
console.log(pets);
5 changes: 5 additions & 0 deletions lesson1/challenges/revising-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
10 changes: 7 additions & 3 deletions lesson1/challenges/rounding-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
*/

var roundUp = 1.5;
var rounded = Math.round(roundUp);
console.log(rounded);
25 changes: 24 additions & 1 deletion lesson2/challenges/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
*/

//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");
}
11 changes: 10 additions & 1 deletion lesson2/challenges/counterServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@
Write a HTTP server application which responds with how many times clients have visited.

Test: $ curl http://localhost:3000
*/
*/

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);
7 changes: 7 additions & 0 deletions lesson2/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var http = require('http');

var server = http.createServer(function (req, res){
res.end('Hello world');
});

server.listen(3000);
19 changes: 13 additions & 6 deletions lesson3/challenges/convertBlocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
})
19 changes: 19 additions & 0 deletions lesson3/challenges/copyFile.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
}
});
9 changes: 9 additions & 0 deletions lesson3/challenges/hello-copy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
1 change: 1 addition & 0 deletions lesson3/demo/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, This is Dan Rathers Speaking.
1 change: 1 addition & 0 deletions lesson3/demo/ltcs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eat,Sleep,Play
60 changes: 60 additions & 0 deletions lesson3/demo/tasks.js
Original file line number Diff line number Diff line change
@@ -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();
}
29 changes: 29 additions & 0 deletions lesson3/demo/writetest.js
Original file line number Diff line number Diff line change
@@ -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");
Binary file added lesson4/.DS_Store
Binary file not shown.
16 changes: 11 additions & 5 deletions lesson4/challenges/chatEmitter.js
Original file line number Diff line number Diff line change
@@ -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.
chat.emit('message', 'ben');
// 6. Finally, Emit the 'message' event twice on the chat object passing in any messages.
chat.emit('join', 'dale');
25 changes: 14 additions & 11 deletions lesson4/challenges/refactorHttpServer.js
Original file line number Diff line number Diff line change
@@ -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');
console.log('Server running at http://localhost:8080');
Binary file added lesson4/demo/.DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions lesson4/demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Foobar</title>
</head>
<body>
<h1 style="font-size:30vw;">Trolololol</h1>

<button id="myBtn">Click me!</button>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
window.alert('clicked!');
console.log('clicked!');
});
</script>
</body>
</html>
Loading