-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commiting challenges.js & answers.md #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,35 @@ | ||
| # 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. Describe some of the differences between `.forEach` & `.map`. | ||
|
|
||
| .forEach is a loop that applies an operation to each list item. ForEach does not return something. | ||
| .map is a loop that creates a new list after performing a transformative task on each list item. Map returns something. | ||
|
|
||
| # 2. Name five different Types in JavaScript. A Type is something that can represent data. What is so special about Arrays? | ||
|
|
||
| String | ||
| Number | ||
| Boolean | ||
| Null | ||
| Undefined | ||
| Symbols (added in ES6) | ||
|
|
||
| Arrays can store multiple data types. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm looking here for this: to add on top of what you've said. Arrays are special types of Objects in JS. |
||
|
|
||
| # 3. What is closure? Can you code out a quick example of a closure? | ||
|
|
||
| In JavaScript, functions are not Just functions, they are Also closures. What that means is that the function body has access to variables that are defined Outside of the function. | ||
|
|
||
| let me = 'Austin'; | ||
| function greatMe() { | ||
| console.log(me + 'Says Hello!'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beautiful explanation! |
||
| } | ||
|
|
||
| This function is able to, in real time, look outside of its own scope and find the variable 'me.' | ||
| # 4. Describe the four rules of the 'this' keyword. No need to provide examples about it this time :) | ||
|
|
||
| Principle 1: Window/Global Binding - When in the global scope, the value of 'this' will be inside the window/console object. | ||
| Principle 2: Implicit Binding - Whenever a function is called by a preceding dot, the object before that dot is this. | ||
| Principle 3: New binding - Whenever a constructor function is used, this refers to the specific instance of the object that is created and returned by the constructor function. | ||
| Principle 4: Explicit binding - Whenever JavaScript’s call or apply method is used, this is explicitly defined. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,32 @@ | |
| 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 outputArray = []; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| outputArray[i] = cb(elements[i]); | ||
| } | ||
| return outputArray; | ||
| }; | ||
|
|
||
| /* ======================== Closure Practice ============================ */ | ||
| /* ======================== Closure Practic | ||
| e ============================ */ | ||
| const limitFunctionCallCount = (cb, n) => { | ||
| // Should return a function that invokes `cb`. | ||
| // The returned function should only allow `cb` to be invoked `n` times. | ||
| let callCount = 0; | ||
| return (...args) => { | ||
| if (callCount === n) return null; | ||
| callCount++; | ||
| return cb(...args); | ||
| }; | ||
| }; | ||
|
|
||
| const cacheFunction = cb => { | ||
|
|
@@ -30,6 +45,10 @@ const cacheFunction = cb => { | |
| 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! | ||
| if (str === '') { | ||
| return ''; | ||
| } | ||
| return reverseStr(str.substr(1)) + str.charAt(0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| }; | ||
|
|
||
| const checkMatchingLeaves = obj => { | ||
|
|
@@ -40,6 +59,15 @@ const checkMatchingLeaves = obj => { | |
| const flatten = elements => { | ||
| // Flattens a nested array (the nesting can be to any depth). | ||
| // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; | ||
| let newArr = []; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (Array.isArray(elements[i])) { | ||
| newArr = newArr.concat(flatten(elements[i])); | ||
| } else { | ||
| newArr.push(elements[i]); | ||
| } | ||
| } | ||
| return newArr; | ||
| }; | ||
|
|
||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this. I would add onto it, that it's primary just a function to iterate over a list and callback the elements with the index. Just maybe swapping the term 'operation' to 'callback' seems a bit better.