Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bloominstituteoftechnology/Sprint-Challenge--JavaScript
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: bitcointroy/Sprint-Challenge--JavaScript
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 10 commits
  • 2 files changed
  • 1 contributor

Commits on Dec 8, 2017

  1. Copy the full SHA
    4d5241c View commit details
  2. Types in short answers.

    bitcointroy committed Dec 8, 2017
    Copy the full SHA
    064297a View commit details
  3. Copy the full SHA
    3fe985f View commit details
  4. Closure.

    bitcointroy committed Dec 8, 2017
    Copy the full SHA
    f9a8c26 View commit details
  5. this keyword - and done.

    bitcointroy committed Dec 8, 2017
    Copy the full SHA
    9e2f8d1 View commit details
  6. each

    bitcointroy committed Dec 8, 2017
    Copy the full SHA
    1f9e640 View commit details
  7. map

    bitcointroy committed Dec 8, 2017
    Copy the full SHA
    0f27359 View commit details
  8. Copy the full SHA
    9cc9042 View commit details
  9. reverse str

    bitcointroy committed Dec 8, 2017
    Copy the full SHA
    3b54c57 View commit details
  10. Copy the full SHA
    06e5319 View commit details
Showing with 73 additions and 10 deletions.
  1. +51 −0 Answers.md
  2. +22 −10 src/challenges.js
51 changes: 51 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# Your responses to the short answer questions should be laid out here using Mark Down.
### For help with markdown syntax [Go here](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

## 1. Differences between `.forEach` & `.map`.
#### Output
`.map` will output a new array using the results of the callback function invoked
`.forEach` will always output `undefined`, although the callback function may output anything: A new array,
an object, strings, or numbers.
#### Use Cases
`.map` can be used almost anywhere in the processing of an array, since it outputs an array, which can be
passed into another (callback) function on down the chain of an algorithm or process.
`.forEach`, since it has no output other than `undefined`, will be most useful as the last step in a process
or in some sort of side process.

## 2. Types (primatives)
There are seven (7) data types as of ECMAScript 6:
1. Boolean: true/false
2. Null: null
3. Undefined: kind of an empty placeholder, to be defined / assigned later
4. Number: numbers, for math and stuff
5. String: typically letters, but sometimes numbers too if they aren't going to be used in calculations
6. Symbol (new as of ECMAScript 6)
7. Object (including Arrays)

#### What's so special about Arrays?
1. Ryan **LOVES** them!
2. Arrays are in the inheritance chain of a program (courtesy of `prototype`), which allows commands like
`indexOf`, `push`, `pop`, etc to assist with managing lists and sets of data.

## 3a. What is Closure?

Closure is the end of the compiler's search for the value of a variable called inside a function. When the
compiler is presented a variable (`x`) which is not declared in the same Scope as where it is called, it must
search additional layers of Scope to find `x`. If the variable declaration is not found in any layer of Scope
accessible to the function where `x` was called, an error will result. If it is, we have CLOSURE!

## 3b. Coding Example of Closure

function doSomething() {
let thing = 'Play fetch'; // thing is in the local Scope of doSomething
function withSomeone(name) {
console.log(`I like to ${thing} with ${name}.`); // thing is called in a different Scope
}
}

The compiler must go 'up' one layer in Scope to find the declaration of thing in order to achieve Closure.

## 4. Four rules of the `this` keyword

1. When a function is contained in the Global Scope, `this` refers to Global/console/window.
2. When a function is called with a preceeding dot, the object before the dot is `this`.
3. When a Constructor Function is used, `this` refers to the Specific Instance of the object created.
4. When `.call` or `.apply` are used, `this` is explicitly defined.
32 changes: 22 additions & 10 deletions src/challenges.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
/* ======================== CallBacks Practice ============================ */
const each = (elements, cb) => {
// Iterates over a list of elements, yielding each in turn to the `cb` function.
// This only needs to work with arrays.
for (let i = 0; i < elements.length; i++) {
cb(elements[i], i);
}
};

const map = (elements, cb) => {
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
const newArray = [];
each(elements, (item, index) => {
newArray.push(cb(item, index));
});
return newArray;
};

/* ======================== Closure Practice ============================ */
const limitFunctionCallCount = (cb, n) => {
// Should return a function that invokes `cb`.
// The returned function should only allow `cb` to be invoked `n` times.
let count = 0; // initialize counter
return (...args) => { // return a function with unknown number of arguments
if (n === count) return null; // limit the counter to a max of n
count++;
return cb(...args); // invoke callback with all the arguments
};
};

const cacheFunction = cb => {
@@ -28,18 +36,22 @@ const cacheFunction = cb => {

/* ======================== Recursion Practice ============================ */
const reverseStr = str => {
// reverse str takes in a string and returns that string in reversed order
// The only difference between the way you've solved this before and now is that you need to do it recursivley!
var first = str[0];
var last = str[str.length -1];
var str1 = reverseStr(str.substring(1, str.length - 1));
return last + str1 + first;
};
// based on https://stackoverflow.com/questions/4859208/recursive-string-reversal-function-in-javascript

const checkMatchingLeaves = obj => {
// return true if every property on `obj` is the same
// otherwise return false
};

const flatten = elements => {
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
each(elements, element => { // call each function to iterate over the array
elements = flatten([].concat([], ...elements)); // flatten by breaking out nested arrays as we recursively call flatten function
});
};

module.exports = {