diff --git a/find-findindex.js b/find-findindex.js new file mode 100644 index 0000000..8dcefac --- /dev/null +++ b/find-findindex.js @@ -0,0 +1,30 @@ +/* +Write a function called `findUserByUsername` which accepts an array of objects, each with a key of username, and a string. The function should return the first object with the key of username that matches the string passed to the function. If the object is not found, return undefined. + +const users = [ + {username: 'mlewis'}, + {username: 'akagen'}, + {username: 'msmith'} +]; + +findUserByUsername(users, 'mlewis') // {username: 'mlewis'} +findUserByUsername(users, 'taco') // undefined +*/ + + +function findUserByUsername(usersArray, username) {} + +/* +Write a function called `removeUser` which accepts an array of objects, each with a key of username, and a string. The function should remove the object from the array. If the object is not found, return undefined. + +const users = [ + {username: 'mlewis'}, + {username: 'akagen'}, + {username: 'msmith'} +]; + +removeUser(users, 'akagen') // {username: 'akagen'} +removeUser(users, 'akagen') // undefined +*/ + +function removeUser(usersArray, username) {} \ No newline at end of file diff --git a/find-findindex.test.js b/find-findindex.test.js new file mode 100644 index 0000000..75210d5 --- /dev/null +++ b/find-findindex.test.js @@ -0,0 +1,40 @@ +describe("#findUserByUsername", function() { + let users; + beforeEach(function() { + users = [ + { username: "mlewis" }, + { username: "akagen" }, + { username: "msmith" } + ]; + }); + it("returns the object if the username matches the string passed", function() { + expect(findUserByUsername(users, "akagen")).toEqual({ username: "akagen" }); + }); + it("returns undefined if a username is not found", function() { + expect(findUserByUsername(users, 'taco')).toEqual(undefined); + }); +}); + +describe("#removeUser", function() { + let users; + beforeEach(function(){ + users = [ + { username: "mlewis" }, + { username: "akagen" }, + { username: "msmith" } + ]; + }) + it("removes a user from an array", function() { + removeUser(users, "mlewis"); + expect(users.length).toEqual(2) + + }); + it("returns the removed user", function() { + expect(removeUser(users,"mlewis")).toEqual({ username: "mlewis" }); + }); + it("returns undefined a user from an array", function() { + expect(removeUser(users, "taco")).toEqual(undefined); + expect(users.length).toEqual(3); + + }); +}); diff --git a/index.html b/index.html new file mode 100644 index 0000000..4a3c20c --- /dev/null +++ b/index.html @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + +