7. Advanced JavaScript
Activity 8: Creating a User Tracker
Solution
- Open the 
Activity08.jsfile and definelogUser. It will add the user to theuserListargument. Make sure no duplicates are added:function logUser(userList, user) { if(!userList.includes(user)) { userList.push(user); } }Here, we used an
includesmethod to check whether the user already exists. If they don't, they will be added to our list. - Define 
userLeft. It will remove the user from theuserListargument. If the user doesn't exist, it will do nothing:function userLeft(userList, user) { const userIndex = userList.indexOf(user); if (userIndex >= 0) { Â Â Â Â userList.splice(userIndex, 1); } }Here, we are using
indexOfto get the current index of the user we want to remove. If the item doesn't exist,indexOfwillreturn –1, so we are only usingspliceto remove the item if it exists. - Define 
numUsers, which returns the number of users currently inside the list...