Skip to content
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

Rework _.extend #1

Open
gregthompson27 opened this issue Sep 6, 2023 · 1 comment
Open

Rework _.extend #1

gregthompson27 opened this issue Sep 6, 2023 · 1 comment

Comments

@gregthompson27
Copy link

underpants/underpants.js

Lines 569 to 605 in 8f52447

_.extend = function (collection, func) {
//this line of code checks if function is provided
if (!func) {
//this line of coded iterates through each element in the collection
for (var item of collection) {
//this line checks if element is false
if (item) {
//return false if at least one element is false
return false;
}
}
//returns true if all elements are true
return true;
}
//this line checks if colllection is an array
if (Array.isArray(collection)) {
//iterates through each element of the array
for (let i = 0; i < collection.length; i++) {
if (!func(collection[i], i, collection)) {
return false;
}
}
} else if (typeof collection === "object") {
//iterates through each property in the obj
for (var key in collection) {
//checks if property is directly on the obj
if (collection.hasOwnProperty(key)) {
//calling the function with the correct arguments
if (!func(collection[key], key, collection)) {
return false;
}
}
}
}
return true;
}

@khamal22, your tests aren't passing for _.extend. I'm not really sure why you're using func as the parameter - the expected arguments for _.extend are an object, and any number of other objects.

A couple things you'll want to research to help implement a solution to this problem:

  1. How do we handle an unknown number of arguments?
  2. Which method can be used to copy properties of one object to another?
@khamal22
Copy link
Owner

I think that I fixed the issues with this code.

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

No branches or pull requests

2 participants