Step 1
- Create a
usersvariable and assign it an empty array literal. - Create a
displaySelectedUserarrow function - Create a
letsCalculateBMIarrow function - Create a
powerupTheUIarrow function - Within the
startAppfunction, invoke thepowerupTheUIfunction, followed by thefetchAndDisplayUsersfunction.
Step 2
In the powerupTheUI function, and using only the DOM Selector API :
- Set
displaySelectedUseras achangeevent listener for the SELECT UI element. - Set
letsCalculateBMIas theclickevent listener for the#oracleBUTTON
Step 3
The fetchAndDisplayUsers tries to call a displayUsers function that does not exist. Lets fix that!
- Create a
displayUsersarrow function just above thefetchAndDisplayUsersfunction. It should take ausersparameter - It should use the array
.forEachfunction to iterate overusersand populate the SELECT UI element with OPTION elements. Each OPTION should have its value set to theidof a given user, while its display text should be set to the user’sname - We can see a sample user in our app; a certain Charles Odili from Nigeria! Add a new user object (e.g representing yourself) to the
usersarray infetchAndDisplayUsers. Feel free to not use real data values, but ensure the id is different from that of the existing user.
Step 4
When we select the sample user from the list, nothing happens. Let’s fix that!
- Create a
getSelectedUserarrow function abovedisplaySelectedUser. It should take auserIdparameter and use the Array.findfunction on theuserscollection to find and return the selected user object . Your.findcall should take an inline arrow function and de-structure its parameter to obtain theidproperty.
Step 5
Recall that the
displaySelectedUserfunction is called as an event handler. This means it will receive aneventobject when the DOM event is triggered.
- Update the
displaySelectedUserfunction and de-structure the expectedeventparameter to just thetargetproperty. - Next,
displaySelectedUsershould callgetSelectedUserwith a certain property oftargetthat represents the selected value of the SELECT element from thechangeevent. Feel free to do some Googling if you dont know the property in question. Finally, assign the result of your.getSelectedUsercall to auservariable. - Next, use
Object.keys(..)to get the collection of properties ofuser. Assign it to apropertiesvariable. - Iterate over
propertieswith the array.forEachfunction, and display the properties in the UI. *Hint: * a given property like Age has a corresponding SPAN element with adata-age-valueattribute. You can use ES6 template strings to build the query selector targeting the SPAN for that property, and then query the DOM with it. You also want to make sure you only update the UI if the DOM query was successful. Good luck!