To complete Milestone 4, you need to implement two key functionalities: adding a selected bird to the favorites list and hiding the "Add to Favorites" button if the length of the favorites list exceeds 3.
Here's how you can achieve that:
- Modify the
updateFavorites
function to actually add the selected bird (based on the currentindex
) to thefavorites
array when the "Add to Favorites" button is clicked. - Implement the logic to hide the "Add to Favorites" button when the favorites list length exceeds 3.
Here's the modified code with the necessary changes:
// 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 = [];
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();
});
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) { // Only add if favorites are less than 3
favorites.push(names[index]); // Add the current bird to favorites
}
updateFavorites();
});
function updateFavorites() {
setText("favorites_output", favorites.join("\n\n"));
// Milestone 4
if (favorites.length > 3) {
hideElement("favorite_button"); // Hide button if favorites exceed 3
} else {
showElement("favorite_button"); // Show button if favorites are 3 or less
}
updateNotification();
}
function updateNotification() {
showElement("notification_label");
// Milestone 6
}
onEvent("insert_button", "click", function() {
// Milestone 7
// Milestone 8
if (true) {
// Code for Milestone 8 placeholder
}
updateFavorites();
});
Key Changes Made:
-
Adding to Favorites:
- Inside the favorite button click event, I check if the favorites list length is less than 3 before adding the current bird to the
favorites
array. - Use
favorites.push(names[index]);
to add the currently viewed bird to the favorites.
- Inside the favorite button click event, I check if the favorites list length is less than 3 before adding the current bird to the
-
Hiding the Favorite Button:
- Adjusted the condition in
updateFavorites()
to check if the length offavorites
exceeds 3 and hide the button accordingly.
- Adjusted the condition in
With this code, when you click the "Add to Favorites" button for three different birds, you should see the button disappear after adding the third bird to the favorites list.