Skip to content

Solving Project 2 Problems#1

Merged
paulghaddad merged 3 commits intomasterfrom
project-2
Sep 26, 2017
Merged

Solving Project 2 Problems#1
paulghaddad merged 3 commits intomasterfrom
project-2

Conversation

@paulghaddad
Copy link
Copy Markdown
Owner

No description provided.

src/project-2.js Outdated
const getBiggest = (x, y) => {
// x and y are integers. Return the larger integer
// if they are the same return either one
return x > y ? x : y;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner:

const getBiggest = (x, y) => x > y ? x : y;

src/project-2.js Outdated
}
};

const isTenOrFive = (num) => {
Copy link
Copy Markdown
Collaborator

@chichiwang chichiwang Sep 25, 2017

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner:

const isTenOrFive = num => num === 10 || num === 5 ? true : false;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

👍 And even simpler: const isTenOrFive = num => (num === 10 || num === 5);

src/project-2.js Outdated

const returnFirst = (arr) => {
// return the first item from the array
return arr.shift();
Copy link
Copy Markdown
Collaborator

@chichiwang chichiwang Sep 25, 2017

Choose a reason for hiding this comment

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

Opportunity for refactor - mutation. shift mutates the parameter passed in and causes this method to have side effects. When working functionally, avoid all side-effects.

const returnFirst = arr => arr[0];

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Great catch!

src/project-2.js Outdated

const returnLast = (arr) => {
// return the last item of the array
return arr.pop();
Copy link
Copy Markdown
Collaborator

@chichiwang chichiwang Sep 25, 2017

Choose a reason for hiding this comment

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

Opportunity for refactor - mutation. pop mutates the parameter passed in and causes this method to have side effects. When working functionally, avoid all side-effects.

const returnLast = arr => arr[arr.length - 1];

src/project-2.js Outdated

const getArrayLength = (arr) => {
// return the length of the array
return arr.length;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const getArrayLength = arr => arr.length;

src/project-2.js Outdated

const isInRange = (num) => {
// return true if num is less than 50 and greater than 20
if (num < 50 && num > 20) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner:

const isInRange = num => num < 50 && num >20 ? true : false;

src/project-2.js Outdated
// arr is an array of integers
// increase each integer by one
// return the array
const incrementedArray = arr.map(element => element += 1);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const incrementByOne = arr => arr.map(el => el + 1);

src/project-2.js Outdated
const addItemToArray = (arr, item) => {
// add the item to the end of the array
// return the array
arr.push(item);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - mutation. push mutates the parameter passed in and causes this method to have side effects. When working functionally, avoid all side-effects.

const addItemToArray = (arr, item) => [...arr, item];

src/project-2.js Outdated
// add the item to the front of the array
// return the array
// hint: use the array method .unshift
arr.unshift(item);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - mutation. unshift mutates the parameter passed in and causes this method to have side effects. When working functionally, avoid all side-effects.

const addItemToFront = (arr, item) => [item, ...arr];

src/project-2.js Outdated
// spaces need to be between each word
// example: ['Hello', 'world!'] -> 'Hello world!'

return words.join(' ');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const wordsToSentence = words => words.join(' ');

src/project-2.js Outdated
const contains = (arr, item) => {
// check to see if item is inside of arr
// return true if it is, otherwise return false
const elementPresent = arr.indexOf(item);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const contains = (arr, item) => arr.indexOf(item) >= 0;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Used includes instead:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

const contains = (arr, item) => arr.includes(item);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

includes is fine for the exercise, but we will still be using indexOf for a long time in production (as it is not supported in our favorite browser as of yet).

src/project-2.js Outdated
const addNumbers = (numbers) => {
// numbers is an array of integers.
// add all of the integers and return the value
return numbers.reduce((sum, num) => sum + num);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const addNumbers = numbers => numbers.reduce((sum, num) => sum + num);

src/project-2.js Outdated
const averageTestScore = (testScores) => {
// testScores is an array. Iterate over testScores and compute the average.
// return the average
const sum = testScores.reduce((memo, num) => memo + num);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const averageTestScore = testScores => addNumbers(testScores)/testScores.length;

const largestNumber = (numbers) => {
// numbers is an array of integers
// return the largest integer
return numbers.reduce((largestItem, num) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Opportunity for refactor - One-liner

const largestNumber = numbers.reduce((num1, num2) => Math.max(num1, num2));

@chichiwang
Copy link
Copy Markdown
Collaborator

chichiwang commented Sep 25, 2017

@paulghaddad Very solid work - mostly just take advantage of lambda syntax wherever possible. It's hard to grok at first, but we may as well get used to functional notation.

On a side note, at our shop we've made the decision to skip arrow notation in favor of function notation except where we explicitly want the behavior of arrow notation.

We spent the period of a week sharing resources and a meeting discussing it: https://developer.ibm.com/node/2015/09/21/an-introduction-to-javascript-es6-arrow-functions/

Arrow notation applies a lexical this and binds the context in which it is declared. It is essentially Function.proptotype.bind behavior. We decided we should only use it when we explicitly want that behavior (React Class methods that are event handlers, for instance).

Arrow notations also do not benefit from hoisting. We've seen arguments in favor of and against this behavior.

Also anonymous arrow functions (and anonymous functions in general) leave a more confusing stacktrace.

Benchmarking arrow notation against the function keyword, I found that in browsers across the board, the function keyword outperforms arrow notation.

@paulghaddad
Copy link
Copy Markdown
Owner Author

@chichiwang Fantastic review! I learned a lot! Just updated the PR. Thank you so much! :)

src/project-2.js Outdated

return arr;
};
const incrementByOne = arr => arr.map(element => element += 1);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No need to update element here, as the result is implicitly returned from the arrow function anyway. element + 1 will work and skip the assignment.

@paulghaddad paulghaddad merged commit 90b6964 into master Sep 26, 2017
@paulghaddad paulghaddad deleted the project-2 branch September 26, 2017 12:57
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.

2 participants