-
Notifications
You must be signed in to change notification settings - Fork 22
Ahmad's Underpants #15
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some food for thought on your code
// Adds first 'num' values to items | ||
var items = []; | ||
for (var i = 0; i < num; i++) { | ||
items.push(arr[i]); | ||
} | ||
|
||
// Returns new array | ||
return items; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AFord504 is there a way you could implement the native .slice method to complete this without using a loop?
// Adds last 'num' values to items | ||
var items = []; | ||
for (var i = arr.length - num; i < arr.length; i++) { | ||
items.push(arr[i]) | ||
} | ||
|
||
// Returns new array | ||
return items; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as above - how might you implement the native .slice method to to solve this part of _.last()
?
if (Array.isArray(coll)) { | ||
_.each(coll, (element, index, array) => output.push(func(element, index, array))); | ||
} else if (_.typeOf(coll) === 'object') { | ||
for (var [key, value] of Object.entries(coll)) { | ||
output.push(func(value, key, coll)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the if statement here necessary - didn't you write some similar logic already inside your _.each method to check for object literals vs. arrays?
var output = _.map(arr, function (element) { | ||
// Iterates over objects in 'arr' | ||
for (var [key, value] of Object.entries(element)) { | ||
// Finds 'prop' and returns its value | ||
var val; | ||
key === prop ? val = value : null; | ||
return val; | ||
} | ||
}); | ||
|
||
// Returns a new array | ||
return output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var output = _.map(arr, function (element) { | |
// Iterates over objects in 'arr' | |
for (var [key, value] of Object.entries(element)) { | |
// Finds 'prop' and returns its value | |
var val; | |
key === prop ? val = value : null; | |
return val; | |
} | |
}); | |
// Returns a new array | |
return output; | |
return _.map(arr, function (element) { | |
// Iterates over objects in 'arr' | |
for (var [key, value] of Object.entries(element)) { | |
// Finds 'prop' and returns its value | |
var val; | |
key === prop ? val = value : null; | |
return val; | |
} | |
}); |
It is possible, and is not a bad coding practice at all, to just return a map like this directly.
No description provided.