Bootcamp Prep – Functions and Scope Labs

Bootcamp Prep – Functions and Scope Labs

This covers the different labs within the Functions and Scope section (within JavaScript Fundamentals module) of Learn.co’s Bootcamp Prep.


Intro to Functions Lab

Task: Get a string and convert it to uppercase or lowercase with functions and return the string/push it to console.log. Then, determine what the string is (uppercase, lowercase, or a specific value) and return one of three responses.

var uppercase, lowercase

function shout(string){
  uppercase = string.toUpperCase();
  string = uppercase;
  return uppercase;
}
function whisper(string){
  lowercase = string.toLowerCase();
  string = lowercase;
  return lowercase;
}
function logShout(string){
  uppercase = string.toUpperCase()
  string = uppercase;
  console.log(uppercase);
}
function logWhisper(string){
  lowercase = string.toLowerCase()
  string = uppercase;
  console.log(lowercase);
}
function sayHiToGrandma(string){
	if (string.toLowerCase() === string){
	  return 'I can\'t hear you!';
	}
	if (string.toUpperCase() === string){
	  return 'YES INDEED!';
	}
	if (string === 'I love you, Grandma.'){
	  return 'I love you, too.';
	}
}

Functions Lab

Task: Create four functions – one each showing a slightly different way to pass arguments and return them/strings.

//1. Write a function that returns a value
function happyHolidays(){
  return "Happy holidays!";
}
//Write a function that takes in a parameter
function happyHolidaysTo(name){
  return `Happy holidays, ${name}!`;
}
//Write a function that takes in multiple parameters
function happyHolidayTo(holiday, name){
  return `Happy ${holiday}, ${name}!`;
}
//Write a function that takes in multiple parameters
function holidayCountdown(holiday, days){
  return `It's ${days} days until ${holiday}!`;
}

JavaScript Arithmetic Lab

Task: Do addition, subtraction, multiplication, and division via functions. Increment and decrement through a function call. Finally, parse a string value as an integer and as a float and return that integer/float.

var number;
function add(a, b){ //add a and b and return the result.
  number = a + b;
  return number;
}
function subtract(a, b){ //subtract a and b and return the result.
  number = a - b;
  return number;
}
function multiply(a, b){ //multiply a and b and return the result.
  number = a * b;
  return number;
}
function divide(a, b){ //divide a and b and return the result.
  number = a / b;
  return number;
}
function inc(a){ //Increment a by 1 and return the result.
  number = a + 1;
  return number;
}
function dec(a){ //Decrement a by 1 and return the result.
  number = a - 1;
  return number;
}
function makeInt(a){ //Parse a as an integer and return that integer.
  number = parseInt(a, 10);
  return number;
}
function preserveDecimal(a){ //Parse a as a float (number with decimals) and return that number.
  number = parseFloat(a, 10);
  return number;
}

Fix the Scope Lab

Task: Fix the problems with scope in the code.  This involved adding “var animal” declaration into yourAnimal() function. Another issue was with the order of lines in add2(n). const two needed to be set to 2 first before it could return anything other than “undefined.”

The hardest thing for me to grasp was the final challenge – getting to the inner function of funkyFunction. Basically, we declare that theFunk is equal to funkyFunction(). Once we have that stored, we can call the var as a function (theFunk() ) and have that result equal theFunk. By doing so we can finally access return function(), which returns “FUNKY!”

var animal = 'dog'

function myAnimal() {
  return animal
}

function yourAnimal() {
  var animal = 'cat'
  // How can we make sure that this function
  // and the above function both pass?
  // P.S.: You can't just hard-code 'cat' below
  return animal
}

function add2(n) {
  const two = 2
  return parseInt(n, 10) + two

  // Feel free to move things around!

}

var funkyFunction = function() {
  return function() {
    return "FUNKY!"
  }
}

// We want 'funkyFunction' on the line below to return a function that returns "FUNKY!" -- how can we accomplish that?
// NOTE: To pass this final test, you only need to modify the code below this line.
var theFunk = funkyFunction()
theFunk = theFunk()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s