✅ Do This - Milestone 6 / 9

Note: You can click the instructions below to expand them and see more specific instructions

Tasks:

Display one of the elements in your notifications list randomly when a user adds a bird to their favorites list
👉 Show me where 👈
Use the setText() function to display a random message from the notifications list in the notification_label element.
randomNumber() takes two arguments: a minimum number and a maximum number.
Set the minimum number to be 0 and the maximum to be notifications.length - 1.
Example: setText("notification_label", notifications[randomNumber(0, notifications.length - 1)]);
🎯 Test Your Code

Note: You can click the tests below to expand them and see more specific instructions

Start the app and click the “Add to Favorites” button. The text for the notification should change at random.
Another way to test would be to press ‘Run” and then to go to the Debug Console. Next to the > character, write updateNotification(). Look on the app’s screen to see if the message changes. Do this several times to test.

// Milestone 1
var names = getColumn("100 Birds of the World", "Name");
var pictures = getColumn("100 Birds of the World", "Image of Bird");
var index = 0;
var favorites = [];

// Milestone 5
var notifications = ["Nice choice! 😊", "That's a beautiful bird!", "You're building a great collection! 🤩"];

updateHomeScreen();

function updateHomeScreen() {
hideElement("notification_label");



// Milestone 2
setText("name_label", names[index]);
setImageURL("bird_Image", pictures[index]);
}
onEvent("right_button", "click", function( ) {
// Milestone 3
if(index < names.length - 1) {
index = index + 1;

}
updateHomeScreen();
updateNotification();
});

onEvent("left_button", "click", function( ) {
// Milestone 3
if (index > 0) {
index = index - 1;

}
updateHomeScreen();
});

onEvent("favorite_button", "click", function( ) {
// Milestone 4
if (favorites.length < 3)
favorites.push(names[index]);
notifications.push("Added to favorites: " + names[index]);
updateFavorites();
});

function updateFavorites() {
setText("favorites_output", favorites.join("\n\n"));
// Milestone 4
if(favorites.length > 3){
showElement("favorite_button");
} else {
hideElement("favorite_button");
}

updateNotification();

}

function updateNotification() {
showElement("notification_label");
// Milestone 6
setText("notification_label", notifications[notifications.length - 1]);
}

onEvent("insert_button", "click", function( ) {
// Milestone 7
notifications.push("Insert button clicked.");
// Milestone 8
if(true){

}

updateFavorites();
});

1 answer

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:

  1. Show Notification Label: The call to showElement("notification_label"); ensures that the notification label is visible to the user.

  2. Select Random Notification: The code notifications[randomNumber(0, notifications.length - 1)] retrieves a random notification from the notifications 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

  1. 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 the notifications list.

  2. 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.

Similar Questions
  1. Do This - Milestone 5 / 9Note: You can click the instructions below to expand them and see more specific instructions Tasks: Add
    1. answers icon 5 answers
  2. ✅ Do This - Milestone 7 / 9Note: You can click the instructions below to expand them and see more specific instructions Tasks:
    1. answers icon 1 answer
  3. ✅ Do This - Milestone 8 / 9Note: You can click the instructions below to expand them and see more specific instructions Tasks:
    1. answers icon 1 answer
  4. ✅ Do This - Milestone 4 / 9Note: You can click the instructions below to expand them and see more specific instructions Tasks:
    1. answers icon 3 answers
more similar questions