Published
Edited
Sep 7, 2019
Insert cell
Insert cell
Insert cell
Insert cell
function blackBox(input1, input2) {
for (var i=0; i<input1; i+= 2)
{
if(input2%19===0)
{
return input2;
}
else
{
input2 = input2-i;
}
}
return "Function Failed"
}
Insert cell
function blackBox2(input1, input2) {
var obj = {
name: input1,
isCrazy: input2,
getsLoopyOnFrootLoops: function(cerealBrand) {
if(cerealBrand==='Froot Loops' && this.isCrazy===true)
return true;
return false;
}
}
return obj;
}
Insert cell
//test functions here
Insert cell
Insert cell
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
iifeExample = {
var ret;
(function iife() {
var mySecret = "dont let the global scope know about this"
ret = "return " + "\"" + mySecret + "\"";
})()
return ret
}
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
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
Insert cell
CatFactory = function(name, age, breed) {
var ownerName = "the wild";
var hasOwner = false;
function introduce(){
if(hasOwner === false){
return "My name is " + name + ", a " + age + " year old " + breed +" with no owner";
}else{
return "My name is " + name + ", a " + age + " year old " + breed +" owned by " + ownerName;
}}
function getOwner(){return ownerName;}
function setOwner(o_o){ownerName = o_o;hasOwner = true;}
function getOlder(){age++;}
return{
introduce: introduce,
getOwner: getOwner,
setOwner: setOwner,
getOlder: getOlder
}
}
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 = (function NumberModule(){
var x = "";
function getVal(){if(x !== ""){return x;}else{return undefined;}}
function setVal(y){x = y; return x;}
function ad(z){x += z; return x;}
function div(u){x = x/u; return x;}
function min(i){x -= i; return x;}
function mult(j){x = x*j; return x;}
return{
getValue: function(){return getVal();},
setValue: function(y){return setVal(y);},
add: function(y){return ad(y);},
divide: function(y){return div(y);},
minus: function(y){return min(y);},
times: function(y){return mult(y);}
}
})()
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

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