Conversation
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; |
There was a problem hiding this comment.
Opportunity for refactor - One-liner:
const getBiggest = (x, y) => x > y ? x : y;
src/project-2.js
Outdated
| } | ||
| }; | ||
|
|
||
| const isTenOrFive = (num) => { |
There was a problem hiding this comment.
Opportunity for refactor - One-liner:
const isTenOrFive = num => num === 10 || num === 5 ? true : false;There was a problem hiding this comment.
👍 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(); |
There was a problem hiding this comment.
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];
src/project-2.js
Outdated
|
|
||
| const returnLast = (arr) => { | ||
| // return the last item of the array | ||
| return arr.pop(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(' '); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Opportunity for refactor - One-liner
const contains = (arr, item) => arr.indexOf(item) >= 0;There was a problem hiding this comment.
Used includes instead:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
const contains = (arr, item) => arr.includes(item);
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
Opportunity for refactor - One-liner
const largestNumber = numbers.reduce((num1, num2) => Math.max(num1, num2));|
@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 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 |
|
@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); |
There was a problem hiding this comment.
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.
0daf3e8 to
e1c23a8
Compare
No description provided.