Skip to content

Commit

Permalink
bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
shakty committed May 7, 2021
1 parent a44e8f3 commit 7022a1a
Show file tree
Hide file tree
Showing 24 changed files with 256 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data/*

*.mp3
*.ogg
*.csv

npm-debug.log

Expand Down
16 changes: 14 additions & 2 deletions 3_NPM/1_intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

// Add something here.

const isOdd = require('is-odd');
let odd = 1;
console.log(isOdd(odd));


// c. Now, make these lines work...

// Add something here.

let even = 2;
var isEven = require('is-even');
var even = 2;
console.log(isEven(even));

// EXERCISE 2. Make the one-liners.
Expand All @@ -40,6 +41,17 @@ console.log(isEven(even));
// Now, I know you know how to do it, so why don't you write down the two
// one-liners that reproduce the methods isOdd and isEven used above?

let isOdd = function(num) {
return num % 2 !== 0;
};


let num = 2;
let isOdd = (num) => num % 2 !== 0;

console.log('Is the number odd? ' + (isOdd(num) ? 'Yes' : 'No'));



// EXERCISE 3. Understanding the packaging.
///////////////////////////////////////////
Expand Down
52 changes: 30 additions & 22 deletions 3_NPM/2_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,36 @@
// For you it is enough to know that it returns a function.
// Execute it and solve the mistery.

// b. Optional. If you are really cool, you might use the path module
// to create cross-platform paths.

// Ref: https://nodejs.org/api/path.html

// EXERCISE 2. Export your own file.
////////////////////////////////////

// Sometimes one export needs to return multiple variables, and not just
// one function as in the "misteryFile.js" of Exercise 1.
// Create a new file inside the lib/ folder and name it "persons.js". This
// file must export two objects containing persons metadata of the type
//
// { first: 'Brendan', last: 'Eich' }
//
// { first: 'Bill', last: 'Gates' }
//
// and one method to get a random person object. In other words, the code below
// must run.

// Hint: the code below use object destructuring, ref:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
// const mistery = require('./lib/misteryFile.js');
// mistery();

// // b. Optional. If you are really cool, you might use the path module
// // to create cross-platform paths.

// const path = require('path');
// let myPath = path.resolve(__dirname, 'lib', 'misteryFile.js');
// console.log(myPath);


// // Ref: https://nodejs.org/api/path.html

// // EXERCISE 2. Export your own file.
// ////////////////////////////////////

// // Sometimes one export needs to return multiple variables, and not just
// // one function as in the "misteryFile.js" of Exercise 1.
// // Create a new file inside the lib/ folder and name it "persons.js". This
// // file must export two objects containing persons metadata of the type
// //
// // { first: 'Brendan', last: 'Eich' }
// //
// // { first: 'Bill', last: 'Gates' }
// //
// // and one method to get a random person object. In other words, the code below
// // must run.

// // Hint: the code below use object destructuring, ref:
// // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

let { person1, person2, getRandomPerson } = require('./lib/persons.js');

Expand Down
14 changes: 14 additions & 0 deletions 3_NPM/lib/persons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let person1 = { first: 'Brendan', last: 'Eich'};

let person2 = { first: 'Bill', last: 'Gates' };

module.exports = {

person1: person1,

person2: person2,

getRandomPerson: function(again) {
return Math.random < 0.5 ? person1 : person2
}
};
93 changes: 92 additions & 1 deletion 3_NPM/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions 3_NPM/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"is-even": "^1.0.0",
"is-odd": "^3.0.1",
"node-fetch": "^2.6.1"
}
}
13 changes: 13 additions & 0 deletions 4_Async/1_async101.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
// Ref: https://www.w3schools.com/jsref/met_win_settimeout.asp
// Hint2: With arrow functions it can be a one-liner.

setTimeout(() => console.log("Hello Brendan!"), 2000);


// b. If you solved exercise 1.a correctly you did a mistake. It is not
// Brendan, it is Bill. Luckily, you are fast to realize your mistake.
// You decide to cancel the timeout before it expires and create a new one
// that says "Hello Bill!" instead.

let timeout = setTimeout(() => console.log("Hello Brendan!"), 2000);
clearTimeout(timeout);
timeout = setTimeout(() => console.log("Hello Bill!"), 2000);


// c. Bonus. Now you want to repeat exercise 1.b, but this time you want
// to say hello to Bill without clearing the timeout.
Expand All @@ -35,10 +41,17 @@
// a. You are pissed off because Bill did not say hello back.
// So you want to obsessively repeat "Hello Bill!" every second.

let interval = setInterval(() => {
console.log('Hello Bill!');
}, 1000);



// Hint: setInterval
// Ref: https://www.w3schools.com/jsref/met_win_setinterval.asp
// Hint2: Ctrl-C (or Apple-C) on console will terminate the infinite salutation.


// b. Bill, shocked by your compulsive reaction, finally says "Hello..." after
// 10 seconds. You can then clear the interval.

9 changes: 6 additions & 3 deletions 4_Async/2_callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let printNews = (allNews) => {
allNews.forEach(news => console.log(`- Breaking News: ${news.title}`));
};

let getLatestNews = () => {
// Imagine to connect to a server and getting back the
let getLatestNews = (cb) => {
// Imagine to connect to a server and getting back the news.
setTimeout(() => {
let news = [
{
Expand All @@ -32,7 +32,10 @@ let getLatestNews = () => {
}
];
console.log('News fetched, now I can print them.');

if (cb) cb(news);

}, 2000);
};

getLatestNews(printNews);
getLatestNews();
11 changes: 9 additions & 2 deletions 4_Async/3_promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Catch the meaning of this intro after you understand promises.
// Finally you will be really happy.

// If you understand JS promises you will undertand it.
// If you understand JS promises you will understand it.

// EXERCISE 1. Promises.
////////////////////////
Expand All @@ -33,7 +33,7 @@
let comfortObj = 1/3;

let promise = new Promise(function(resolve, reject) {
if (Math.random() > comfortObj) resolve(true);
if (Math.random() <= comfortObj) resolve(true);
else reject(false);
});

Expand All @@ -54,6 +54,11 @@ let comfortObj = 1/3;

promise = new Promise(function(resolve, reject) {
// Write the body of this promise.
setTimeout(() => {
console.log('ASYNC');
if (Math.random() <= comfortObj) resolve(true);
else reject(false);
}, 2000);
});

console.log('Do you sleep with a Teddy bear?');
Expand Down Expand Up @@ -86,6 +91,8 @@ promise
.catch(res => console.log(`No, you are not.`))
.finally(() => {
// Something here.
exerciseIsOver = true;
console.log('Is exercise over?', '1', '2', '3', '...', 'Yes!');
});


6 changes: 4 additions & 2 deletions 4_Async/5_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const fetch = require("node-fetch");
const ENDPOINT = "https://swapi.dev/api/";

// Change me.
let query = 'YOU_NEED_TO_CHANGE_THIS';
let query = 'people/1/';

fetch(ENDPOINT + query)
.then(res => {
Expand All @@ -54,11 +54,13 @@ fetch(ENDPOINT + query)
// Fetches gets the headers first, then it process
// the body asynchronously.

// console.log(res);

// It also returns a promise.
return res.json();
})
.then(json => {
console.log(json);
console.log('We got: ' + json.name);
})
.catch(err => {
console.error(err);
Expand Down
5 changes: 4 additions & 1 deletion 4_Async/solutions/5_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ let query = 'people/1';
// If needed, you may create an anonimous async function.

(async() => {

try {
const res = await fetch(ENDPOINT + query);

Expand All @@ -90,12 +91,13 @@ let query = 'people/1';

const user = await res.json();

console.log(user);
console.log('We got ASYNC/AWAIT: ', user.name);

}
catch(err) {
console.error(err);
}

})();

// Exercise 3. Optional. Fetch them all.
Expand Down Expand Up @@ -135,6 +137,7 @@ let doFetch = (page = 1) => {
})
.then(json => {
db = [ ...db, ...json.results ];
// db = db.concat(json.results);
doFetch(++page);
})
.catch(err => {
Expand Down
2 changes: 1 addition & 1 deletion 5_Browser/1_html101.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script>

// Uncomment for step-by-step execution.
// debugger;
debugger;

// Locate the <DIV> tag.

Expand Down
Loading

0 comments on commit 7022a1a

Please sign in to comment.