Published
Edited
Sep 9, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// DECLARAING A FUNCTION
function myFunctionThatCalculatesASum( parameter1, parameter2 ) {
// CODE BLOCK: do stuff in here
var sumOfMyParamters = parameter1 + parameter2
return sumOfMyParamters // <-- this is outputted from the function
}
Insert cell
// RUNNNING THE FUNCTION
theOutput = myFunctionThatCalculatesASum( 5 , 10 )
Insert cell
{
function sum(a,b) {
return a + b
}
function product(a,b) {
return a * b
}
var onePlusTwo = sum(1,2)
var threePlusFour = sum(3,4)
var productOfTwoVariables = product(onePlusTwo, threePlusFour)
return productOfTwoVariables // <-- ignore that return. it's to make the notebook work
}
Insert cell
Insert cell
{
var bob = function () {return "Hi, I'm Bob! And I'm two years old. "}
var joe = bob() + " And now I'm called by Joe!"
return joe
}
Insert cell
Insert cell
function checkDay(today) {
if(today === "Sunday" || today === "Saturday") {
return "WEEKEND"
} else {
return "WEEKDAY"
}
}
Insert cell
checkDay("Monday")
Insert cell
Insert cell
{
// COUNTING TO TEN
var count = 0
while( count != 10 ) {
count = count + 1;
}
return count;
}
Insert cell
md` With while loops, we can fall into an "infinite loop" trap, where the loop repeats forever. Let's try to make the example code above into an infinite loop`
Insert cell
Insert cell
Insert cell
{
// COUNTING TO TEN
var count = 0
for( let i = 0; i < 10; i++ ) {
count = count + 1
}
return count
}
Insert cell
Insert cell
Insert cell
function testable(isGood) {
if(isGood===true)
return "yes"
return "no"
}
Insert cell
test1 = testable(true)==="yes"
Insert cell
test2 = testable(false)==="no"
Insert cell
test3 = testable(1)==="yes"
Insert cell
Insert cell
Insert cell
function sleepIn(weekday, vacation) {
if (weekday === true){
if(vacation === true){
return true;
}else{return false;}
}else{
if(vacation === true){
return true;
}else{return true;}
}
}
Insert cell
{
if(sleepIn(1,2)===undefined)
return "Your function will be unit tested here"
try
{
expect(sleepIn(true,true)).to.equal(true)
expect(sleepIn(true,false)).to.equal(false)
expect(sleepIn(false,true)).to.equal(true)
expect(sleepIn(false,false)).to.equal(true)
return success()
}
catch(err)
{
return failure()
}
}
Insert cell
Insert cell
function hasTeen(num1, num2, num3) {
if(num1 >= 13 && num1 <= 19){
return true;
} else if (num2 >= 13 && num2 <=19){
return true;
} else if (num3 >= 13 && num3 <=19){
return true;
} else {return false;}
}
Insert cell
{
if(hasTeen(1,1,1)===undefined)
return "Your function will be unit tested here"
try
{
expect(hasTeen(0,0,0)).to.equal(false)
expect(hasTeen(0,13,0)).to.equal(true)
expect(hasTeen(12,12,12)).to.equal(false)
expect(hasTeen(11,19,20)).to.equal(true)
expect(hasTeen(14,18,0)).to.equal(true)
expect(hasTeen(10,20,12)).to.equal(false)
expect(hasTeen(19,19,19)).to.equal(true)
return success()
}
catch(err)
{
return failure()
}
}
Insert cell
Insert cell
function front3(str) {
var newStr = "";
if(str.length > 3){
for(var i = 0; i < 3; i++){
newStr += str.substring(0,3);
}
}else{
for(var i = 0; i < 3; i++){
newStr += str;
}
}
return newStr;
}
Insert cell
{
if(front3("abc")===undefined)
return "Your function will be unit tested here"
try
{
expect(front3("")).to.equal("")
expect(front3("abc")).to.equal("abcabcabc")
expect(front3("XYZ")).to.equal("XYZXYZXYZ")
expect(front3("az")).to.equal("azazaz")
expect(front3("bob")).to.equal("bobbobbob")
expect(front3("NaN")).to.equal("NaNNaNNaN")
expect(front3("123")).to.equal("123123123")
expect(front3("zzz")).to.equal("zzzzzzzzz")
return success()
}
catch(err)
{
return failure()
}
}
Insert cell
Insert cell
function cashCheck(checkAmount)
{
var num100s = 0;
var num50s = 0;
var num20s = 0;
var num10s = 0;
var num5s = 0;
var num1s = 0;
var numbertypes = 0;
var str = "Give the customer ";
while(checkAmount > 0){
if(checkAmount >=100){
num100s++;
checkAmount = checkAmount - 100;
}else if(checkAmount >= 50){
num50s++;
checkAmount = checkAmount - 50;
} else if(checkAmount >= 20){
num20s++;
checkAmount = checkAmount - 20;
} else if(checkAmount >= 10){
num10s++;
checkAmount = checkAmount - 10;
}
else if(checkAmount >= 5){
num5s++;
checkAmount = checkAmount - 5;
} else if(checkAmount >= 1){
num1s++;
checkAmount = checkAmount - 1;
}
}
if(num100s != 0){numbertypes++;}
if(num50s != 0){numbertypes++;}
if(num20s != 0){numbertypes++;}
if(num10s != 0){numbertypes++;}
if(num5s != 0){numbertypes++;}
if(num1s != 0){numbertypes++;}
var fP = true;
if(num100s == 1){
str = str + "1 $100 bill";
numbertypes--;
fP = false;
}else if(num100s > 1){
str = str + num100s + " $100 bills";
numbertypes--;
fP = false;
}
if(fP == false){
if(numbertypes > 0){str += ", ";}
if(numbertypes == 0){str += ".";}
}
if(num50s == 1){
str = str + "1 $50 bill";
numbertypes--;
fP = false;
}else if(num50s > 1){
str = str + num50s + " $50 bills";
numbertypes--; fP = false;
}
if(fP == false && num50s > 0){
if(numbertypes > 0){str += ", ";}
if(numbertypes == 0){str += ".";}
}
if(num20s == 1){
str = str + "1 $20 bill";
numbertypes--; fP = false;
}else if(num20s > 1){
str = str + num20s + " $20 bills";
numbertypes--; fP = false;
}
if(fP == false && num20s > 0){
if(numbertypes > 0){str += ", ";}
if(numbertypes == 0){str += ".";}
}
if(num10s == 1){
str = str + "1 $10 bill";
numbertypes--; fP = false;
}else if(num10s > 1){
str = str + num10s + " $10 bills";
numbertypes--; fP = false;
}
if(fP == false && num10s > 0){
if(numbertypes > 0){str += ", ";}
if(numbertypes == 0){str += ".";}
}
if(num5s == 1){
str = str + "1 $5 bill";
numbertypes--; fP = false;
}else if(num5s > 1){
str = str + num5s + " $5 bills";
numbertypes--; fP = false;
}
if(fP == false && num5s > 0){
if(numbertypes > 0){str += ", ";}
if(numbertypes == 0){str += ".";}
}
if(num1s == 1){
str = str + "1 $1 bill";
numbertypes--; fP = false;
}else if(num1s > 1){
str = str + num1s + " $1 bills";
numbertypes--; fP = false;
}
if(fP == false && num1s > 0){
if(numbertypes > 0){str += ", ";}
if(numbertypes == 0){str += ".";}
}
var ripu = str.lastIndexOf(",");
if(ripu > 0){
var halp1 = str.substring(0, ripu + 1) + " and" + str.substring(ripu + 1);
return halp1;
}else{
return str;}
}
Insert cell
checkAmount = 27//check your code here
Insert cell
Insert cell
{
if(cashCheck(12)==='Give the customer 12 dollars.')
return "Your function will be unit tested here"
try
{
expect(cashCheck(20)).to.equal('Give the customer 1 $20 bill.')
expect(cashCheck(17)).to.equal('Give the customer 1 $10 bill, 1 $5 bill, and 2 $1 bills.')
expect(cashCheck(298)).to.equal('Give the customer 2 $100 bills, 1 $50 bill, 2 $20 bills, 1 $5 bill, and 3 $1 bills.')
expect(cashCheck(1052)).to.equal('Give the customer 10 $100 bills, 1 $50 bill, and 2 $1 bills.')
return success()
}
catch(err)
{
return failure()
}
}
Insert cell
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