To implement the functionality where a random notification message is displayed when a user adds a bird to their favorites list, you need to make a couple of changes in your updateNotification()
function. Specifically, you'll use the randomNumber()
function to select a random notification.
Here’s how you can modify the updateNotification()
function:
function updateNotification() {
showElement("notification_label");
// Milestone 6
setText("notification_label", notifications[randomNumber(0, notifications.length - 1)]);
}
Explanation:
-
Show Notification Label: The call to
showElement("notification_label");
ensures that the notification label is visible to the user. -
Select Random Notification: The code
notifications[randomNumber(0, notifications.length - 1)]
retrieves a random notification from thenotifications
array:randomNumber(0, notifications.length - 1)
generates a random index within the bounds of the notifications array.
Full Modified Code
Here’s how your complete updateNotification()
function should look after this modification:
function updateNotification() {
showElement("notification_label");
// Set a random notification message
setText("notification_label", notifications[randomNumber(0, notifications.length - 1)]);
}
Testing the Functionality
-
Add to Favorites: When you press the "Add to Favorites" button, the
updateNotification()
function is called, and it should now display a random message from thenotifications
list. -
Debug Console: You can test this by typing
updateNotification()
in the Debug Console to see if the message changes randomly.
With these changes, your application will now provide a new random notification whenever a bird is added to the favorites list.