Published
Edited
Feb 18, 2019
Insert cell
Insert cell
scopesExample = {
// if we ignore the globalScopeExample = { } above...
var imGlobal = "global variable"
function localScope() {
var imLocal = "local variable"
imGlobal // global can be accessed in here.
}
localScope()
// global variable can be accessed over here but not local variable
return [ imGlobal, imLocal ]
}
Insert cell
Insert cell
locallyScopedFunctions = {
function globalFunction() {
function localFunction() {
return "Hai from local function"
}
return "Hai from global function"
}
globalFunction()
localFunction()
}
Insert cell
Insert cell
function Poodle(name, owner) {
var breed = "poodle"
var sound = "Woof"
function introduce() {
return "I'm a " + breed + " named " + name +
". My owner is " + owner + "."
}
function setSound(s) {
sound = s
}

return {
talk: function() {
return sound + "! " + introduce()
},
setSound: setSound
}
}
Insert cell
{
var 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
iifeExample = {
var myLetter
;(function iife() {
var mySecret = "dont let the global scope know about this"
var myRandomNumber = Math.floor(Math.random() * (mySecret.length - 1))
myLetter = mySecret[ myRandomNumber ]
})()
return "My letter is " + myLetter
}
Insert cell
Insert cell
tipCalculator = (function TipCalculator() {
// anything we need to do to set up the calculator goes here
function roundToHundreths(num) {
return Math.round(100*num) / 100
}

function findPercentage(percent, amount) {
return roundToHundreths(amount * percent);
}

return {
// the calculator functions we choose to "expose" to the user
fifteen: function(amount) {
return "$" + findPercentage(0.15, amount)
},
twenty: function(amount) {
return "$" + findPercentage(0.2, amount)
},
twentyFive: function(amount) {
return "$" + findPercentage(0.25, amount)
},
thirty: function(amount) {
return "$" + findPercentage(0.3, amount)
}
}
})()
Insert cell
tipCalculator.fifteen(12.57)
Insert cell
tipCalculator.twenty(57.09)
Insert cell
Insert cell
dog = ({
type:"dog",
age: 0,
sound: "woof",
breed: "unknown",
})
Insert cell
{
var poodle = {
name: "Marvin",
age: 2,
breed: "poodle",
speak: function() {
return this.sound;
}
}
Object.setPrototypeOf(poodle, dog)
return "This " +
poodle.type + " is a " +
poodle.breed + " named " +
poodle.name + " who says " +
poodle.speak()
}
Insert cell
{
var pug = {
name: "George",
age: 5,
breed: "pug",
speak: function() {
return this.sound;
}
}
Object.setPrototypeOf(pug, dog)
return "This " +
pug.type + " is a " +
pug.breed + " named " +
pug.name + " who says " +
pug.speak()
}
Insert cell
Insert cell
{
var pug = {
name: "George",
age: 5,
breed: "pug",
speak: function() {
return this.sound;
}
}
Object.setPrototypeOf(pug, dog)
return pug.__proto__ === dog
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
var alfred = CatFactory( "Alfred", 3, "calico" )
if(alfred === undefined) return md`Your answer will be checked here`
expect(alfred.getOwner()).to.equal("the wild")
expect(alfred.introduce()).to.equal("My name is Alfred, a 3 year old calico with no owner")
expect(alfred.setOwner( "John" )).to.equal(undefined)
expect(alfred.getOwner()).to.equal("John")
expect(alfred.owner).to.equal(undefined)
expect(alfred.getOlder()).to.equal(undefined)
expect(alfred.introduce()).to.equal("My name is Alfred, a 4 year old calico owned by John")
var alfonse = CatFactory( "Alfonse", 9, "siamese" )
expect(alfonse.getOwner()).to.equal("the wild")
expect(alfonse.introduce()).to.equal("My name is Alfonse, a 9 year old siamese with no owner")
expect(alfonse.setOwner( "Jack" )).to.equal(undefined)
expect(alfonse.getOwner()).to.equal("Jack")
expect(alfonse.owner).to.equal(undefined)
expect(alfonse.getOlder()).to.equal(undefined)
expect(alfonse.introduce()).to.equal("My name is Alfonse, a 10 year old siamese owned by Jack")

return success()
}
Insert cell
Insert cell
numberModule = undefined // replace with your code here
Insert cell
{
if(numberModule === undefined) return md`Your answer will be checked here`
expect(numberModule.getValue()).to.equal(undefined)
expect(numberModule.setValue(0)).to.equal(0)
expect(numberModule.getValue()).to.equal(0)
expect(numberModule.add(100)).to.equal(100)
expect(numberModule.divide(5)).to.equal(20)
expect(numberModule.minus(5)).to.equal(15)
expect(numberModule.getValue()).to.equal(15)
expect(numberModule.times(4)).to.equal(60)
expect(numberModule.setValue(220)).to.equal(220)
expect(numberModule.getValue()).to.equal(220)
expect(numberModule.divide(11)).to.equal(20)
expect(numberModule.minus(2)).to.equal(18)
expect(numberModule.divide(3)).to.equal(6)
expect(numberModule.getValue()).to.equal(6)
expect(numberModule.times(4)).to.equal(24)
expect(numberModule.add(7)).to.equal(31)
return success()
}
Insert cell
Insert cell
Insert cell
// ^ Use that object above as your prototype
fooze = {
var fooze = {
// your code here
}
// your code here
// this return is just for the notebook to work. nothing to see here.
return fooze
}
Insert cell
{
if( Object.keys(fooze).length === 0 ) return md`Your answer will be checked here`
expect(fooze.name).to.equal("Fooze")
expect(fooze.age).to.equal(3)
expect(fooze.breed).to.equal("German Shepard")
expect(fooze.bark()).to.equal("Woof")
expect(doggo.name).to.equal("No name")
expect(doggo.age).to.equal(-1)
expect(fooze).to.not.have.own.property('breed');
expect(fooze).to.not.have.own.property('bark');
expect(fooze).to.not.have.own.property('sound');
expect(doggo.isPrototypeOf(fooze)).to.equal(true)
return success()
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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