diff --git a/3-getting-started/1-executing-scripts/1-hello-world.js b/3-getting-started/1-executing-scripts/1-hello-world.js new file mode 100644 index 0000000..77f7fdb --- /dev/null +++ b/3-getting-started/1-executing-scripts/1-hello-world.js @@ -0,0 +1,9 @@ +import { createServer } from 'http'; + +const server = createServer((req, res) => { + res.end('My First Web Server\n'); +}); + +server.listen(4242, () => { + console.log('Server is running...'); +}); diff --git a/3-getting-started/1-executing-scripts/2-print-line.js b/3-getting-started/1-executing-scripts/2-print-line.js new file mode 100644 index 0000000..868d219 --- /dev/null +++ b/3-getting-started/1-executing-scripts/2-print-line.js @@ -0,0 +1 @@ +console.log('Pluralsight rocks'); diff --git a/3-getting-started/2-timers/1-setTimeout-example.js b/3-getting-started/2-timers/1-setTimeout-example.js new file mode 100644 index 0000000..c555a98 --- /dev/null +++ b/3-getting-started/2-timers/1-setTimeout-example.js @@ -0,0 +1,6 @@ +setTimeout( + () => { + console.log('Hello after 4 seconds'); + }, + 4 * 1000 +); diff --git a/3-getting-started/2-timers/2-setTimeout-with-function.js b/3-getting-started/2-timers/2-setTimeout-with-function.js new file mode 100644 index 0000000..ea6884b --- /dev/null +++ b/3-getting-started/2-timers/2-setTimeout-with-function.js @@ -0,0 +1,8 @@ +const func = () => { + console.log('Hello after 4 seconds'); +}; + +setTimeout(func, 4 * 1000); + +// For: func(arg1, arg2, arg3, ...) +// We can use: setTimeout(func, delay, arg1, arg2, arg3, ...) diff --git a/3-getting-started/2-timers/3-setTimeout-with-function-and-arguments.js b/3-getting-started/2-timers/3-setTimeout-with-function-and-arguments.js new file mode 100644 index 0000000..87ef3f6 --- /dev/null +++ b/3-getting-started/2-timers/3-setTimeout-with-function-and-arguments.js @@ -0,0 +1,5 @@ +const rocks = who => { + console.log(who + ' rocks'); +}; + +setTimeout(rocks, 2 * 1000, 'Pluralsight'); diff --git a/3-getting-started/2-timers/4-challenge1.js b/3-getting-started/2-timers/4-challenge1.js new file mode 100644 index 0000000..87d5b27 --- /dev/null +++ b/3-getting-started/2-timers/4-challenge1.js @@ -0,0 +1,17 @@ +// You can define only ONE function +const helloFunc = (delay) => { + console.log(`Hello! after ${delay}`) +} + +// This is if you're using another function to make it more readable +// const theOneFunc = (timeout) => { +// setTimeout(helloFunc, timeout * 1000, timeout); +// }; + +// Hello after 4 seconds +setTimeout(helloFunc, 4 * 1000, 4); + +// Hello after 8 seconds +setTimeout(helloFunc, 8 * 1000, 8); + + diff --git a/3-getting-started/2-timers/4-solution1.js b/3-getting-started/2-timers/4-solution1.js new file mode 100644 index 0000000..0da4b3c --- /dev/null +++ b/3-getting-started/2-timers/4-solution1.js @@ -0,0 +1,7 @@ +const theOneFunc = delay => { + console.log('Hello after ' + delay + ' seconds'); +}; + +setTimeout(theOneFunc, 4 * 1000, 4); + +setTimeout(theOneFunc, 8 * 1000, 8); diff --git a/3-getting-started/2-timers/5-setInterval-example.js b/3-getting-started/2-timers/5-setInterval-example.js new file mode 100644 index 0000000..9b08133 --- /dev/null +++ b/3-getting-started/2-timers/5-setInterval-example.js @@ -0,0 +1,4 @@ +setInterval( + () => console.log('Hello every 3 seconds'), + 3000 +); diff --git a/3-getting-started/2-timers/6-clearing-timers.js b/3-getting-started/2-timers/6-clearing-timers.js new file mode 100644 index 0000000..25df833 --- /dev/null +++ b/3-getting-started/2-timers/6-clearing-timers.js @@ -0,0 +1,6 @@ +const timerId = setTimeout( + () => console.log('You will not see this one!'), + 0 +); + +clearTimeout(timerId); diff --git a/3-getting-started/2-timers/7-no-guarantees.js b/3-getting-started/2-timers/7-no-guarantees.js new file mode 100644 index 0000000..e473f28 --- /dev/null +++ b/3-getting-started/2-timers/7-no-guarantees.js @@ -0,0 +1,8 @@ +setTimeout( + () => console.log('Hello after 0.5 seconds. MAYBE!'), + 500, +); + +for (let i = 0; i < 1e10; i++) { + // Block Node Synchronously +} diff --git a/3-getting-started/2-timers/8-challenge2.js b/3-getting-started/2-timers/8-challenge2.js new file mode 100644 index 0000000..b6014be --- /dev/null +++ b/3-getting-started/2-timers/8-challenge2.js @@ -0,0 +1,19 @@ +let counter = 0; + +// Print "Hello World" +// Every second +const interval = setInterval( + () => { + console.log(counter) + console.log('Hello every second'); + counter++; + + // And stop after 5 times + if (counter === 5) { + // After 5 times. Print "Done" and let Node exit. + console.log("Done"); + clearTimeout(interval); + } + }, + 1000 +); diff --git a/3-getting-started/2-timers/8-solution2.js b/3-getting-started/2-timers/8-solution2.js new file mode 100644 index 0000000..afdab47 --- /dev/null +++ b/3-getting-started/2-timers/8-solution2.js @@ -0,0 +1,10 @@ +let counter = 0; +const intervalId = setInterval(() => { + console.log('Hello World'); + counter += 1; + + if (counter === 5) { + console.log('Done'); + clearInterval(intervalId); + } +}, 1000); diff --git a/3-getting-started/2-timers/9-more-challenges.txt b/3-getting-started/2-timers/9-more-challenges.txt new file mode 100644 index 0000000..36e90b7 --- /dev/null +++ b/3-getting-started/2-timers/9-more-challenges.txt @@ -0,0 +1,37 @@ +Challenge 1: + + Print "Hello World" forever. Starting with a delay of 1 second + but then incrementing the delay by 1 second each time. + The second time will have a delay of 2 seconds + The third time will have a delay of 3 seconds. + + Include the delay in the printed message + Hello World. 1 + Hello World. 2 + Hello World. 3 + ... + + Constraints: You can only use const (no let or var) + +Challenge 2: + + Just like Challenge 1 but this time with repeated delay values. + Print Hello World 5 times with a delay of 100 ms. + Then Print it again 5 more times with a delay of 200 ms. + Then print it again 5 more times with a delay of 300 ms. + And so on until the program is killed. + + Include the delay in the printed message: + Hello World. 100 + Hello World. 100 + Hello World. 100 + Hello World. 100 + Hello World. 100 + Hello World. 200 + Hello World. 200 + Hello World. 200 + ... + + Constraints: + - Only use setInterval (not setTimeout) + - Use only ONE if statement diff --git a/3-getting-started/2-timers/9-solution-1.js b/3-getting-started/2-timers/9-solution-1.js new file mode 100644 index 0000000..5ead164 --- /dev/null +++ b/3-getting-started/2-timers/9-solution-1.js @@ -0,0 +1,7 @@ +const greeting = delay => + setTimeout(() => { + console.log('Hello World. ' + delay); + greeting(delay + 1); + }, delay * 1000); + +greeting(1); diff --git a/3-getting-started/2-timers/9-solution-2.js b/3-getting-started/2-timers/9-solution-2.js new file mode 100644 index 0000000..d696562 --- /dev/null +++ b/3-getting-started/2-timers/9-solution-2.js @@ -0,0 +1,16 @@ +let lastIntervalId, counter = 5; + +const greeting = delay => { + if (counter === 5) { + clearInterval(lastIntervalId); + lastIntervalId = setInterval(() => { + console.log('Hello World. ' + delay); + greeting(delay + 100); + }, delay); + counter = 0; + } + + counter += 1; +}; + +greeting(100);