βœ… Do This - Milestone 8 / 9

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

Tasks:

Create a conditional to test if the length of the favorites list is greater than or equal to 3
πŸ‘‰ Show me where πŸ‘ˆ
Replace the true statement in the if-else conditional with an expression that evaluates if the length of the favorites list is greater than 3.
Example: if (favorites.length > 3)
Remove the last item in the favorites list if the length is greater than or equal to 3
Within the conditional, use the removeItem() function to remove the last entry in the favorites list.
Example: removeItem(favorites, favorites.length - 1);
🎯 Test Your Code

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

Within the conditional statement that you just wrote, copy/paste the following statement before the removeItem() function:
console.log("Removing: " + favorites[3]); This will show the bird that is getting removed from the end of the list.
You can also check to see that the length of the favorites list is 3 or less by entering the following command into the Debug Console, next to the > character: favorites.length

// 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[randomNumber(0, notifications.length - 1)]);
var favo
}

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

}

updateFavorites();
});

1 answer

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:

  1. 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) {
    
  2. 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]);
    
  3. Remove the Last Item: The code uses the removeItem() function to remove the last entry in the favorites 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.

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 4 / 9Note: You can click the instructions below to expand them and see more specific instructions Tasks:
    1. answers icon 3 answers
  4. βœ… Do This - Milestone 6 / 9Note: You can click the instructions below to expand them and see more specific instructions Tasks:
    1. answers icon 1 answer
more similar questions