Insert cell
Insert cell
Insert cell
Insert cell
iifeExample = {
let ret;
(() => {
const mySecret = "dont let the global scope know about this"
ret = `return "${mySecret}"`
})()
//notice that we don't have access to mySecret
return ret
}
Insert cell
Insert cell
function Poodle(name, owner) {
const breed = "poodle"
let sound = "Woof"
const introduce = () => `I'm a ${breed} named ${name}. My owner is ${owner}.`
function setSound(s) {
sound = s
}

return {
talk: () => `${sound}! ${introduce()}`,
setSound: setSound
}
}
Insert cell
{
const marvin = Poodle("Marvin", "Mike")
// so we have access to the setSound method
marvin.setSound( "wrrooof" )
// NONE OF THESE WORK
// marvin.owner = "Bob"
// marvin.name = "Spock"
// marvin.breed = "Bulldog"
// marvin.sound = "WRAAA"
// marvin.introduce = undefined
///////////////////////////
// Marvin still says the same thing
return marvin.talk();
}
Insert cell
Insert cell
{
var alfred = Poodle("Alfred", "George")
var sput = Poodle("Sput", "Jack")
var lola = Poodle("Lola", "Matt")
return alfred.talk() + "\n" +
sput.talk() + "\n" +
lola.talk() + "\n"
}
Insert cell
Insert cell
tipCalculator = (() => {
// anything we need to do to set up the calculator goes here
const roundToHundreths = num => Math.round(100*num) / 100

const findPercentage = (percent, amount) => roundToHundreths(amount * percent)

return {
// the calculator functions we choose to "expose" to the user
fifteen: amount => `$${findPercentage(0.15, amount)}`,
twenty: amount => `$${findPercentage(0.20, amount)}`,
twentyFive: amount => `$${findPercentage(0.25, amount)}`,
thirty: amount => `$${findPercentage(0.30, amount)}`
}
})()
Insert cell
tipCalculator.fifteen(12.57)
Insert cell
tipCalculator.twenty(57.09)
Insert cell
Insert cell
Insert cell
CatFactory = function() {
// your code here
}
Insert cell
Insert cell
Insert cell
numberModule = undefined // replace with your code here
Insert cell
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