Skip to content

Instantly share code, notes, and snippets.

@taesup
Last active March 4, 2016 07:28
Show Gist options
  • Save taesup/fdf6a11f3ce3cde819dd to your computer and use it in GitHub Desktop.
Save taesup/fdf6a11f3ce3cde819dd to your computer and use it in GitHub Desktop.
DevLeague-Day2-Prep-Notes-Answers
// ++ NOTES
/**
* console.log() --> PRINT TO SCREEN
* function () {} --> Declare a function (but don't run it)
* {functionName}(arg1, arg2) --> Actually using the function by name
*/
// GLOBAL VARIABLES
var foo = 2;
var bar = 2;
var random = Math.random();
// GLOBAL NAMED FUNCTION
function test(foo, bar) { // arguements
var baz = 3; // local variable
return foo + ' ' + bar + baz;
}
var baz = test(foo, bar);
console.log(baz); // 2 23
// GLOBAL ANONYMOUS FUNCTION
var func = function() {
return foo + bar + random;
};
var bib = func(bar);
console.log(bib); // 4.230480234
// IF EXAMPLE
var test = Math.random(); // something true
if (test) { // truthy outcome
// do this
}
else { // falsy outcome
// do something else
}
// FOR LOOP EXAMPLE
for (var i=0; i<3; i++) {
console.log(i);
}
// WHILE LOOP EXAMPLE
var i = 0;
while (i<3) {
console.log(i);
i++;
}
// ++ Answers
var a = 1;
var b = 2;
// ADD
function add(x, y) {
return x + y;
}
var sum = add(1,2);
console.log(sum);
// subtract
function subtract(x, y) {
return x - y;
}
var difference = subtract(a, b);
console.log(difference);
// multiply
function multiply(x, y) {
return x * y;
}
var product = multiply(a, b);
console.log(product);
// checkDifference
function checkDifference(x) {
return 'My football team lost ' + x + ' times this week';
}
console.log(checkDifference(difference));
// checkSum
function checkSum(x) {
return 'I CAN ADDZ ' + x + ' NUMBERS';
}
console.log(checkSum(sum));
// checkProduct
function checkProduct(x, y) {
return x * y;
}
console.log(checkProduct(product, difference));
// addThenSubtract
function addThenSubtract(x, y, z) {
var first = add(x,y);
var second = subtract(first, z);
return second;
}
console.log(addThenSubtract(5, 2, 3));
// addThenMultiply
function addThenMultiply(x, y, z) {
var first = add(x, y);
var second = multiply(first, z);
return second;
}
var howMany = addThenMultiply(5, 2, 3);
console.log('howMany: ', howMany);
// createFullName
function createFullName(x, y) {
return x + ' ' + y;
}
var myFullName = createFullName('Ed', 'Kim');
console.log(myFullName);
// verifyDrinkingAge
function verifyDrinkingAge(x) {
return x >= 21;
}
var canDrinkBeer = verifyDrinkingAge(34);
// throwParty
function throwParty (x) {
if (x) { return 'This Party will have an open bar'; }
else { return 'The Party will have tons of Cake!'; }
}
console.log(throwParty(canDrinkBeer));
// eatFood
function eatFood(x, y, z) {
var name = createFullName(x, y);
return name + ' loves to eat ' + z;
}
console.log(eatFood('Ed', 'Kim', 'Pizza'));
// repeater
function repeater(x, y) {
for (var i = 0; i < x; i++) {
if (y) { console.log('Bacon Pancakes, makin\' Bacon Pancakes...'); }
else { console.log('Let it go.... LET IT GOOOOOOoOoOoOo...'); }
}
}
repeater(howMany, canDrinkBeer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment