Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Answers.md
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.

Copy link
Copy Markdown
Contributor

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.

.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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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!');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"name": "Sprint-Challenge--JavaScript",
"version": "1.0.0",
"description":
"* The objective of this challenge is to get you used to answering a few questions about JavaScript that are commonly asked in interviews. * We also have some more reps for you to help hammer in the knowledge you've thus far learned. * Answers to your written questions will be recorded in *Answers.md* * This is to be worked on alone but you can use outside resources. You can *reference* any old code you may have, and the React Documentation, however, please refrain from copying and pasting any of your answers. Try and understand the question and put your responses in your own words. Be as thorough as possible when explaining something. * **Just a friendly Reminder** Don't fret or get anxious about this, this is a no-pressure assessment that is only going to help guide you here in the near future. This is NOT a pass/fail situation. ## Start by forking and cloning this repository. ## Questions - Self Study - You can exercise your Google-Fu for this and any other _Sprint Challenge_ in the future. 1. Describe some of the differences between `.forEach` & `.map`. 2. Name five different Types in JavaScript. A Type is something that can represent data. What is so special about Arrays? 3. What is closure? Can you code out a quick example of a closure? 4. Describe the four rules of the 'this' keyword. No need to provide examples about it this time :)",
"description": "* The objective of this challenge is to get you used to answering a few questions about JavaScript that are commonly asked in interviews. * We also have some more reps for you to help hammer in the knowledge you've thus far learned. * Answers to your written questions will be recorded in *Answers.md* * This is to be worked on alone but you can use outside resources. You can *reference* any old code you may have, and the React Documentation, however, please refrain from copying and pasting any of your answers. Try and understand the question and put your responses in your own words. Be as thorough as possible when explaining something. * **Just a friendly Reminder** Don't fret or get anxious about this, this is a no-pressure assessment that is only going to help guide you here in the near future. This is NOT a pass/fail situation. ## Start by forking and cloning this repository. ## Questions - Self Study - You can exercise your Google-Fu for this and any other _Sprint Challenge_ in the future. 1. Describe some of the differences between `.forEach` & `.map`. 2. Name five different Types in JavaScript. A Type is something that can represent data. What is so special about Arrays? 3. What is closure? Can you code out a quick example of a closure? 4. Describe the four rules of the 'this' keyword. No need to provide examples about it this time :)",
"main": "index.js",
"scripts": {
"test": "eslint tests/*.js && eslint src/*.js && jest --verbose",
"watch": "npm test -- --watch"
},
"repository": {
"type": "git",
"url":
"git+https://github.com/LambdaSchool/Sprint-Challenge--JavaScript.git"
"url": "git+https://github.com/LambdaSchool/Sprint-Challenge--JavaScript.git"
},
"keywords": [],
"author": "",
Expand All @@ -31,6 +29,5 @@
"bugs": {
"url": "https://github.com/LambdaSchool/Sprint-Challenge--JavaScript/issues"
},
"homepage":
"https://github.com/LambdaSchool/Sprint-Challenge--JavaScript#readme"
"homepage": "https://github.com/LambdaSchool/Sprint-Challenge--JavaScript#readme"
}
30 changes: 29 additions & 1 deletion src/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! carAt() and substr() both work really well here!

};

const checkMatchingLeaves = obj => {
Expand All @@ -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 = {
Expand Down