Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JavaScript I without stretch questions#298

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mmargo10 wants to merge1 commit intobloominstituteoftechnology:master
base:master
Choose a base branch
Loading
frommmargo10:master
Open
Show file tree
Hide file tree
Changes fromall 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
38 changes: 37 additions & 1 deletionsrc/arrays.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,12 +14,20 @@ const each = (elements, cb) => {
// This only needs to work with arrays.
// You should also pass the index into `cb` as the second argument
// based off http://underscorejs.org/#each
for (let i = 0; i <= elements.length - 1; i++) {
cb(elements[i], i);
}
};

const map = (elements, cb) => {
// Do NOT use .map, to complete this function.
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
// Return the new array
const arr = [];
for (let i = 0; i < elements.length; i++) {
arr.push(cb(elements[i]));
}
return arr;
};

const reduce = (elements, cb, startingValue) => {
Expand All@@ -28,21 +36,49 @@ const reduce = (elements, cb, startingValue) => {
// Elements will be passed one by one into `cb` along with the `startingValue`.
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
let returnValue = '';
if (startingValue === undefined) {
returnValue = elements[0];

for (let i = 1; i < elements.length; i++) {
returnValue = cb(returnValue, elements[i]);
}
} else if (startingValue !== undefined) {
returnValue = startingValue;

for (let i = 0; i < elements.length; i++) {
returnValue = cb(returnValue, elements[i]);
}
}
return returnValue;
};


const find = (elements, cb) => {
// Do NOT use .includes, to complete this function.
// Look through each value in `elements` and pass each element to `cb`.
// If `cb` returns `true` then return that element.
// Return `undefined` if no elements pass the truth test.
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i]) === true) {
return elements[i];
}
}
};


const filter = (elements, cb) => {
// Do NOT use .filter, to complete this function.
// Similar to `find` but you will return an array of all elements that passed the truth test
// Return an empty array if no elements pass the truth test
for (let i = 0; i < elements.length; i++) {
if (cb(elements[i]) === true) {
return elements[i];
}
}
};


/* STRETCH PROBLEM */

const flatten = (elements) => {
Expand Down
14 changes: 14 additions & 0 deletionssrc/callbacks.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
const firstItem = (arr, cb) => {
// firstItem passes the first item of the given array to the callback function.
cb(arr[0]);
};

const getLength = (arr, cb) => {
// getLength passes the length of the array into the callback.
cb(arr.length);
};

const last = (arr, cb) => {
// last passes the last item of the array into the callback.
cb(arr[arr.length - 1]);
};

const sumNums = (x, y, cb) => {
// sumNums adds two numbers (x, y) and passes the result to the callback.
cb(x + y);
};

const multiplyNums = (x, y, cb) => {
// multiplyNums multiplies two numbers and passes the result to the callback.
cb(x * y);
};

const contains = (item, list, cb) => {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
for (let i = 0; i < list.length; i++) {
if (list[i] === item) {
cb(true);
} else {
cb(false);
}
}
};

/* STRETCH PROBLEM */
Expand All@@ -29,6 +41,8 @@ const removeDuplicates = (array, cb) => {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.


};

/* eslint-enable */
Expand Down
13 changes: 13 additions & 0 deletionssrc/closure.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,17 +5,30 @@ const counter = () => {
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
let count = 0;
return () => ++count;
};

const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
let count = 0;
return {
increment: () => ++count,
decrement: () => --count,
};
};

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);
};

Choose a reason for hiding this comment

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

This is exactly the solution code from the solution branch... In the future I'd like to see where you got with your own code.

};

/* STRETCH PROBLEM */
Expand Down
4 changes: 4 additions & 0 deletionssrc/objects.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,22 +5,26 @@ const keys = (obj) => {
// Retrieve all the names of the object's properties.
// Return the keys as strings in an array.
// Based on http://underscorejs.org/#keys
return Object.keys(obj);
};

const values = (obj) => {
// Return all of the values of the object's own properties.
// Ignore functions
// http://underscorejs.org/#values
return Object.values(obj).map(key => obj[key]);

Choose a reason for hiding this comment

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

Object.values(obj); is sufficient the code you currently have actually breaks.

};

const mapObject = (obj, cb) => {
// Like map for arrays, but for objects. Transform the value of each property in turn.
// http://underscorejs.org/#mapObject
return Object.values(obj).map(key => cb(obj[key]));

Choose a reason for hiding this comment

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

There's a few things wrong with this.
I assume you wanted to useObject.keys(obj) if you wanted to access the values, but this should also be building a new object with the same keys, and values that are passed into the callback function (cb). At the moment you're returning an array ofNaN objects (because of the particular callback function).

};

const pairs = (obj) => {
// Convert an object into a list of [key, value] pairs.
// http://underscorejs.org/#pairs
return Object.keys(obj).map(key => [key, obj[key]]);
};

/* STRETCH PROBLEMS */
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp