This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose I want to add two numbers, then double it, then square that number, then add double it again. | |
*/ | |
addTwoNos(2, 3, (sum) => { | |
double(sum, (doubleOfSum) => { | |
square(doubleOfSum, (square) => { | |
double(square, (doubleOfSquare) => { | |
console.log(doubleOfSquare) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose I want to add two numbers, then double it, then square that number, then add double it again. Using Promises. | |
*/ | |
addTwoNos(2, 3) | |
.then(sum => double(sum)) | |
.then(doubleOfSum => square(doubleOfSum)) | |
.then(square => double(square)) | |
.then(output => console.log(output)) | |
.catch(err => console.log(err)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
An aync function that takes two numbers and performs some operations | |
*/ | |
performOperations(2,3).then(output => console.log(output)).catch(err => console.log(err)); | |
async function performOperations(a,b) { | |
let sum = await addTwoNos(a, b); | |
let doubleOfSum = await double(sum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Asynchronous Map Function via async module | |
*/ | |
const Async = require('async'); | |
Async.map([1,2,3], double, (err, results) => { | |
if(err) console.log(err); | |
console.log(results) | |
} ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(() => console.log('this code is executed asynchronously'), 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function playMontyHall(toSwitch) { | |
let carIsIn = Math.floor(Math.random() * 3); | |
let doorSelected = Math.floor(Math.random() * 3); | |
let revealedDoor = [0,1,2].find((door) => door !== carIsIn && door !== doorSelected); | |
if(toSwitch){ | |
return carIsIn === [0,1,2].find((door) => door !== doorSelected && door !== revealedDoor) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function simulateGame(num, toSwitch) { | |
let gamesWon = 0; | |
for(let i = 0; i < num; i++){ | |
gamesWon += playMontyHall(toSwitch) | |
} | |
return gamesWon | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(simulateGame(1000, toSwitch = true)); // play 1000 times, switch the door. Print how many times won. | |
console.log(simulateGame(1000, toSwitch = false)); // play 1000 times, don't switch the door. Print how many times won. | |
/* Output of first line will be close to 666 while that of second will be close to 333. */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
let applications = []; | |
app.use(bodyParser.urlencoded({extended : true})); | |
app.use(bodyParser.json()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
class ResultComponent extends Component { | |
render() { | |
let {result} = this.props; | |
return ( | |
<div className="result"> | |
<p>{result}</p> |
OlderNewer