Published
Edited
Sep 13, 2019
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
review = function(x) {
var name = 'bob';
var number = x;
var callNextNumber = function() {
number --;
return number;
}
var losePlaceInLine = function() {
number = Math.random()*100
return number;
}
var getYourDeliMeat = function(order) {
if(number === 0)
{
return "I got " + order + "!";
}
else
{
return "You tried to cut the line! Shame on you! Your new place in line is: "+losePlaceInLine();
}
}
return {
callNextNumber: callNextNumber,
losePlaceInLine: losePlaceInLine,
getYourDeliMeat: getYourDeliMeat
}
}

Insert cell
Insert cell
Insert cell
Insert cell
{
var romeo = {name: "romeo", hasRose: false}
var juliet = {name: "jules", wantsRose: true}
if(romeo.hasRose===true){
romeo.hasRose=false
juliet.wantsRose=false
return "Happily ever after!"
}
else if(romeo.hasRose===false)
{
return "Romeo fails"
}
romeo.hasRose=true
}
Insert cell
Insert cell
{
var romeo = {name: "romeo", hasRose: false}
var juliet = {name: "jules", wantsRose: true}
if(romeo.hasRose===true){
romeo.hasRose=false
juliet.wantsRose=false
return "Happily ever after!"
}
else if(romeo.hasRose===false)
{
romeo.oweRose=true
juliet.wantsRose=false
}
romeo.hasRose=true
romeo.oweRose=false
romeo.hasRose=false
return "Happily ever after!"
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
promiseFunc = function() {
return new Promise( function(resolve, reject) {
setTimeout(function() {
return resolve(x+50), 1000}
)}
)}
Insert cell
finalResult = (promiseFunc())
.then(x => x%3)
.then(x => x+30)
.catch(err => "I encountered an error: " + err + ". Sorry!")
Insert cell
Insert cell
Insert cell
youPromiseToGiveMeFood = function (typeOfFood) {
return new Promise( function(s,f) {
return s(typeOfFood)}
)
}
Insert cell
iPromiseToPayYouBack = function(amt, from) {
return new Promise( function(s,f) {
return s(from - amt)}
)
}
Insert cell
Insert cell
eatFood = function(myStuff) {
return "I ate the " + myStuff.food
}
Insert cell
checkWhatIHave = function(myStuff) {
return "I have a " + myStuff.food + " and $" + myStuff.wallet + " dollars left."
}
Insert cell
// define the async function
promisedFoodTransaction = async function() {
var myFood;
var myWallet = 100;

// this will return a promise
myFood = youPromiseToGiveMeFood("Lakeview Beef w. Broccoli")
// then it'll keep executing down here
// this will also return a promise
myWallet = /* Promise */ iPromiseToPayYouBack(20, myWallet)
// the promises haven't been resolved, but I'm returning them anyway:
return {
food:myFood,
wallet: myWallet
}
}
Insert cell
Insert cell
promisedFoodTransaction() instanceof Promise
Insert cell
promisedFoodTransaction().then(eatFood)
Insert cell
promisedFoodTransaction().then(checkWhatIHave)
Insert cell
Insert cell
Insert cell
// define the async function
awaitFoodTransaction = async function() {
var myFood;
var myWallet = 100;

// the function will pause at this line below until you give me my food
myFood = await youPromiseToGiveMeFood("Lakeview Beef w. Broccoli")
// then it'll keep executing down here
// and pause until i pay you back
myWallet = await iPromiseToPayYouBack(20, myWallet)
// and now we've finished the transaction
// I return an object containing my belongings
return {
food:myFood,
wallet: myWallet
}
}
Insert cell
awaitFoodTransaction() instanceof Promise
Insert cell
awaitFoodTransaction().then(eatFood)
Insert cell
awaitFoodTransaction().then(checkWhatIHave)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pokemon = 'chikorita'
Insert cell
Insert cell
axios = require("https://unpkg.com/axios/dist/axios.min.js") //this allows our notebook to use the axios library
Insert cell
pokemonUrl = axios.get('https://pokeapi.co/api/v2/pokemon/'+ pokemon)
.then(function(result) {
return result.data.sprites.front_shiny
}).catch(function(error) {
return 'https://buttonologyinc.com/wp-content/themes/buttonology/library/images/404.jpg'
})
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more