Published
Edited
Sep 16, 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 + "\"";
})() //the extra parentheses execute the function
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 //the setSound attribute is the function setSound, so no parentheses needed
}
}
Insert cell
{
var marvin = Poodle("Marvin", "Mike")
// so we have access to the setSound method
marvin.setSound( "wrrooof" )
// NONE OF THESE WORK because these variables are NOT in scope, only marvin.talk() is
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() { //Anonymous function (has parentheses in front of it
// 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(gName, gAge, gBreed) {
var CatFactory = {
name: gName,
age: gAge,
breed: gBreed,
getOwner: function(){
return retrieveOwner();
},
setOwner: function(newbie){
replaceOwner(newbie);
},
getOlder: function(){
this.age +=1;
},
introduce: function(){
var storeOwner = retrieveOwner();
var ownString = '';
if(storeOwner == "the wild"){
ownString = " with no owner"
}
else{
ownString = " owned by " + storeOwner;
}
return "My name is " + this.name + ", a " + this.age + " year old " + this.breed + ownString;
}
}
var owner = 'the wild';
var retrieveOwner = function(){
return owner;
}
function replaceOwner(newOwner){
owner = newOwner;
}
function introduce(){
}
return CatFactory;
}
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() { //Anonymous function (has parentheses in front of it)
var value = undefined;
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
getValue: function(){
return value;
},
setValue: function(newValue){
value = newValue;
return value;
},
add: function(additive){
value += additive;
return value;
},
divide: function(divideBy){
var tempValue = (value / divideBy);
value = tempValue;
return value;
},
minus: function(subtractMe){
value -= subtractMe;
return value;
},
times: function(multiplyBy){
var product = value * multiplyBy;
value = product;
return value;
}
}
})()
Insert cell
{
numberModule.setValue(undefined) //added because of a bug with the value not resetting, idk
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