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
32 changes: 32 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# 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`.
* `.for each()` is a method that calls on a provided function once for each element in an array, and its in order.
* `.map` creates a new array with the results of calling a function for every element in an array, in order.

Both go over each element in an array and in order but map creates a new array when calling a function and forEach just goes over it once not creating a new array.

## 2. Name five different Types in JavaScript. A Type is something that can represent data. What is so special about Arrays?
* String - text
* number- intergers
* boolen - two values either its true or false
* object -collection of properties
* null - will give you null and only null value


## 3. What is closure? Can you code out a quick example of a closure?
Function name(firstName, lastName){
const intro = 'Your name is';
function wholeName() {
return intro + firstName + " " + lastName;
};
return wholeName ();
};


## 4. Describe the four rules of the 'this' keyword. No need to provide examples about it this time :)

* Window/ Global binding - when in the global scope, the value of 'this' will be inside the window/console object.

* Implicit binding- whenever a function is called by a preceding dot what ever is to the left of the period.

* New Binding- Whenever a construtor function is used, this refers to the specific instance of the object that is created and returned by the constructor function.

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"
}
29 changes: 28 additions & 1 deletion src/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@
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 = [];
for (let i = 0; i < elements.length; i++) {
newArray.push(elements[i] + 1);
newArray[i] = cb(elements[i]);
}
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 callCount = 0;
return (x, y, z) => {
if (callCount === n) return null;
callCount++;
return cb(x, y, z);
};
};

const cacheFunction = cb => {
// Should return a funciton that invokes `cb`.
// Should return a function that invokes `cb`.
// A cache (object) should be kept in closure scope.
// The cache should keep track of all arguments have been used to invoke this function.
// If the returned function is invoked with arguments that it has already seen
Expand All @@ -30,6 +47,7 @@ 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!
return str.split('').reverse().join('');
};

const checkMatchingLeaves = obj => {
Expand All @@ -40,6 +58,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