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
30 changes: 30 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# 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)

## Differences from `forEach` and `map`
Answer: `forEach` will iterate array element and you can execute some actions to each element. After the action, the element in the array will be mutated. And you won't get any return value from `forEach`.

While `map` will iterate array element and you can apply some actions to each element. After the action, you will get a new collection of the elements which are transformmed by the action you execute to each of them. `map` will not mutate the original array but return you a new collection of elements.

## Data Type in JavaScript
Answer: `String`, `Number`, `Boolean`, `Array`, `Object`.

## Special About Arrays
Answer: array can hold multiple values under a single variable name with any data type. And array is a special object which uses number index, while object uses string index. Also array has many built-in methods and itself is iterable by indeces.

## What is closure?
Answer: A closure is the combination of a function and a lexical environment within which that function is declared. For example:
```
function sayName(name) {
let name = name;
function sayHello() {
console.log(`Hello ${name}!`);
}
sayHello();
}
sayName('Ting'); // Output will be: 'Hello Ting!'
```
In this example, inner function sayName has no local variable, but when we execute outer function sayName, sayHello can access the variable name outside its scope.
## Describe the 4 rules of this keyword
Answer: 1. when using function keyword to declear the function, inside this function if we call this, that would be return the whole Window object in browser.
2. inside the object if we call this which will refer to the object itself.
3. when using constructor function to build a object, this will be binding to object when the new keyword is used to create a new object where this refers to object itself.
4. when using call, apply, or bind function to a object, this will refer to the object that is filled in as argument inside the call, apply, or bind.
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"
}
52 changes: 52 additions & 0 deletions src/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@
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 newArr = [];
for (let i = 0; i < elements.length; i++) {
newArr.push(cb(elements[i]));
}
return newArr;
};

/* ======================== 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;
return (...args) => {
if (count === n) return null;
count++;
return cb(...args);
};
};

const cacheFunction = cb => {
Expand All @@ -22,6 +36,14 @@ const cacheFunction = cb => {
// If the returned function is invoked with arguments that it has already seen
// then it should return the cached result and not invoke `cb` again.
// `cb` should only ever be invoked once for a given set of arguments.
let cacheArgument;
let cacheResult;
return argument => {
if (cacheArgument === argument && cacheArgument) return cacheResult;
cacheArgument = argument;
cacheResult = cb(argument);
return cacheResult;
};
};

/* eslint-enable no-unused-vars */
Expand All @@ -30,16 +52,46 @@ 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!
let newStr = '';
newStr += str.slice(str.length - 1);
str = str.substr(0, str.length - 1);
if (str.length === 0) return newStr;
return newStr + reverseStr(str);
};

const checkMatchingLeaves = obj => {
// return true if every property on `obj` is the same
// otherwise return false
const valueSet = new Set();
const flattenObject = obj1 => {
let arr = [];
Object.values(obj1).forEach(value => {
if (typeof value === 'object') {
arr = arr.concat(flattenObject(value));
} else {
arr.push(value);
}
});
return arr;
};
flattenObject(obj).forEach(element => {
valueSet.add(element);
});
return valueSet.size === 1;
};

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 arr = [];
elements.forEach(element => {
if (Array.isArray(element)) {
arr = arr.concat(flatten(element));
} else {
arr.push(element);
}
});
return arr;
};

module.exports = {
Expand Down