Skip to content

Conversation

@invegat
Copy link

@invegat invegat commented Oct 2, 2017

No description provided.

// Return the new array.
};

const reduce = (elements, cb, startingValue) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can also do this for a starting value: (elements, cb, startingValue = elements.shift()) for the default value. This takes advantage of the ES6 default parameters.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless of course you are avoiding modifying the input array

src/arrays.js Outdated
// Elements will be passed one by one into `cb`.
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
if (startingValue === undefined) {
startingValue = (typeof elements[0]) === 'string' ? '' : 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just make startingValue the first item from the array

Copy link
Contributor

Choose a reason for hiding this comment

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

A reduce is supposed to have the first item in the array as its initial memo

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.
return reduce(elements, (acc, v) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Really creative

Copy link
Author

Choose a reason for hiding this comment

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

thanks

src/arrays.js Outdated
/* Extra Credit */

function isArray(elements) {
return (Object.prototype.toString.call(elements) === '[object Array]');
Copy link
Contributor

Choose a reason for hiding this comment

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

The preferred way of checking whether a variable is an array or not is this: Array.isArray(myVariable);

Copy link
Contributor

Choose a reason for hiding this comment

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

Good job creating another function though

src/callbacks.js Outdated
// code here

const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'];
function firstItem(foods,f) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer it if you put a space between the parameters

Copy link
Author

Choose a reason for hiding this comment

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

accustomed to getting a 'comma-spacing' error by the eslint configs I have been working with

src/callbacks.js Outdated

const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'];
function firstItem(foods,f) {
return f(foods[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

This line should have a semicolon

src/callbacks.js Outdated
// code here

function getLength(foods,f) {
return f(foods.length)
Copy link
Contributor

@SunJieMing SunJieMing Oct 2, 2017

Choose a reason for hiding this comment

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

Make sure that you stay consistent. Inconsistent code is a mark of an inexperienced developer. It shows experience and discipline to keep your code consistent.

return Object.keys(obj);
};

function isFunction(func) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For the sake of consistency this should be written the same way the other functions are written.

const result = arrayMethods.reduce(arr, callBackMockFn);
expect(result).toBe(25);
expect(callBackMockFn.mock.calls.length).toBe(4);
expect(callBackMockFn.mock.calls.length).toBe(5);
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should not be modified. The memo in the reduce should be the first item from the array and so the callback would only be invoked 4 times.

@sperrye sperrye closed this Oct 13, 2017
Smithface added a commit to Smithface/JavaScript-I that referenced this pull request Jan 10, 2018
RainKins added a commit to RainKins/JavaScript-I that referenced this pull request Jan 10, 2018
RainKins added a commit to RainKins/JavaScript-I that referenced this pull request Jan 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants