Upload code from "Getting Started With Node" module
parent
05e7fcf7c4
commit
7be1ead2c3
@ -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...');
|
||||
});
|
@ -0,0 +1 @@
|
||||
console.log('Pluralsight rocks');
|
@ -0,0 +1,6 @@
|
||||
setTimeout(
|
||||
() => {
|
||||
console.log('Hello after 4 seconds');
|
||||
},
|
||||
4 * 1000
|
||||
);
|
@ -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, ...)
|
@ -0,0 +1,5 @@
|
||||
const rocks = who => {
|
||||
console.log(who + ' rocks');
|
||||
};
|
||||
|
||||
setTimeout(rocks, 2 * 1000, 'Pluralsight');
|
@ -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);
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
const theOneFunc = delay => {
|
||||
console.log('Hello after ' + delay + ' seconds');
|
||||
};
|
||||
|
||||
setTimeout(theOneFunc, 4 * 1000, 4);
|
||||
|
||||
setTimeout(theOneFunc, 8 * 1000, 8);
|
@ -0,0 +1,4 @@
|
||||
setInterval(
|
||||
() => console.log('Hello every 3 seconds'),
|
||||
3000
|
||||
);
|
@ -0,0 +1,6 @@
|
||||
const timerId = setTimeout(
|
||||
() => console.log('You will not see this one!'),
|
||||
0
|
||||
);
|
||||
|
||||
clearTimeout(timerId);
|
@ -0,0 +1,8 @@
|
||||
setTimeout(
|
||||
() => console.log('Hello after 0.5 seconds. MAYBE!'),
|
||||
500,
|
||||
);
|
||||
|
||||
for (let i = 0; i < 1e10; i++) {
|
||||
// Block Node Synchronously
|
||||
}
|
@ -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
|
||||
);
|
@ -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);
|
@ -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
|
@ -0,0 +1,7 @@
|
||||
const greeting = delay =>
|
||||
setTimeout(() => {
|
||||
console.log('Hello World. ' + delay);
|
||||
greeting(delay + 1);
|
||||
}, delay * 1000);
|
||||
|
||||
greeting(1);
|
@ -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);
|
Loading…
Reference in New Issue