To implement the tasks for Milestone 8 as outlined in your instructions, you would implement the conditional statement to check the length of the favorites
list. Then, you would log the bird that is getting removed before removing the last entry in the favorites list using the removeItem()
function.
Hereβs how you can modify your code:
function updateFavorites() {
setText("favorites_output", favorites.join("\n\n"));
// Milestone 4
if (favorites.length > 3) {
showElement("favorite_button");
} else {
hideElement("favorite_button");
}
updateNotification();
}
onEvent("insert_button", "click", function() {
// Milestone 7
notifications.push("Insert button clicked.");
// Milestone 8
if (favorites.length >= 3) {
console.log("Removing: " + favorites[favorites.length - 1]); // Show the bird getting removed
removeItem(favorites, favorites.length - 1); // Remove the last item from the favorites list
}
updateFavorites();
});
Changes Made:
-
Condition to Test List Length: The conditional statement checks if the length of
favorites
is greater than or equal to 3.if (favorites.length >= 3) {
-
Logging the Bird: The code logs the bird's name that is being removed from the favorites list.
console.log("Removing: " + favorites[favorites.length - 1]);
-
Remove the Last Item: The code uses the
removeItem()
function to remove the last entry in thefavorites
list.removeItem(favorites, favorites.length - 1);
Testing Your Code:
Make sure to test the code by clicking the "Insert" button when there are at least three favorites in your list. You can also check the length of favorites
by entering favorites.length
in the Debug Console. After these changes, your program should work as required by the specified milestones.