Last active
June 21, 2023 19:39
-
-
Save LovelaceTuring/d959bcec157c44d131c2490af2628124 to your computer and use it in GitHub Desktop.
Vocabulary for Chapter 3 of Eloquent JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// VOCAB LIST - Chapter 3 | |
/* | |
function: A function is created with an expression that starts with the keyword function. Functions have a set of parameters (in this case, only x) and a body, which contains the statements that are to be executed when the function is called. The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement. | |
return statement: A return statement determines the value the function returns (A return keyword without an expression after it will cause the function to return undefined) | |
Parameters to a function: behave like regular bindings, but their initial values are given by the caller of the function, not the code in the function itself | |
scope: Each binding has a scope, which is the part of the program in which the binding is visible | |
lexical scoping: block in the program text. Each local scope can also see all the local scopes that contain it, and all scopes can see the global scope. Side note: When called, the function body sees the environment in which it was created, not the environment in which it is called. | |
The Call Stack: the way that control flows through functions is involved. Because a function has to jump back to the place that called it when it returns, the computer must remember the context from which the call happened. The place where the computer stores this context is the call stack | |
Optional Arguments/Default Parameters: JS does not care if you pass in the required number of arguments when calling a function. If you write an = operator after a parameter, followed by an expression, the value of that expression will replace the argument when it is not given. | |
a Closure: A function that references bindings from local scopes around it is called a closure... being able to reference a specific instance of a local binding in an enclosing scope—is called closure | |
Recursive: A function that calls itself is called recursive | |
Wishful Programming: You might even start writing code that uses the function before you actually define the function itself. | |
Pure Functions: A pure function is a specific kind of value-producing function that not only has no side effects but also doesn’t rely on side effects from other code—for example, it doesn’t read global bindings whose value might change | |
*/ | |
// Code Examples: | |
// function examples: | |
// assign a function expression to a binding | |
const nameOfFunction = function(parameter) { | |
console.log(parameter + 2); | |
}; | |
// function declaration | |
function nameOfFunction(param1, param2) { | |
return param1 * param2 * 3.5; | |
} | |
// A less verbose function value | |
let nameOfFunction = param => param % 3; | |
// A closure: | |
function multiplier(factor) { | |
return number => number * factor; | |
} | |
let twice = multiplier(2); | |
console.log('definition of twice:', twice); | |
console.log(twice(5)); | |
// → 10 | |
// A recursive solution: | |
function findSolution(target) { | |
function find(current, history) { | |
// if current equals target, return history | |
if (current === target) { | |
return history; | |
// if current is greater than target, return null | |
} else if (current > target) { | |
return null; | |
// if current is less than target | |
} else { | |
// tries both branches, first add 5, then multiply by 3 | |
return find(current + 5, `(${history} + 5)`) || | |
find(current * 3, `(${history} * 3)`); | |
} | |
} | |
// return initial call to "find" | |
return find(1, "1"); | |
} | |
console.log(findSolution(24)); | |
// → (((1 * 3) + 5) * 3) | |
// Excercise Solutions | |
// Minimum | |
function min(param1, param2) { | |
// if param2 is undefined | |
if (param2 === undefined){ | |
// return param1 | |
return param1; | |
} | |
// if param1 is less than param2 | |
if (param1 < param2) { | |
// return param1 | |
return param1; | |
// otherwise | |
} else { | |
// return param2 | |
return param2; | |
} | |
} | |
console.log(min(0, 10)); | |
// → 0 | |
console.log(min(0, -11)); | |
// → -10 | |
// Recursion | |
function isEven(n) { | |
// what if the input is negative | |
if (n < 0) { | |
// make n not negative | |
n = Math.abs(n); | |
} | |
// if n is 0 | |
if (n === 0) { | |
// return true | |
return true; | |
} | |
// if n is 1 | |
if (n === 1) { | |
// return false | |
return false; | |
} | |
// in all other cases, apply isEven to n minus 2 | |
return isEven(n - 2); | |
} | |
console.log(isEven(50)); | |
// → true | |
console.log(isEven(75)); | |
// → false | |
console.log(isEven(-14)); | |
// → ?? | |
// Bean Counting | |
function countBs(string) { | |
// return a call to countChar with input and "B" | |
return countChar(string, 'B'); | |
} | |
function countChar(string, character) { | |
// create a result, set to 0 | |
let result = 0; | |
// loop over the string | |
for (let i = 0; i < string.length; i++) { | |
// if current character matches input character | |
if (string[i] === character) { | |
// increment our result by 1 | |
result = result + 1; | |
} | |
} | |
// return result | |
return result; | |
} | |
console.log(countBs("BBC")); | |
// → 2 | |
console.log(countChar("kakkerlak", "k")); | |
// → 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment