158k views
2 votes
Given:

var vehicles = ["boat", "car", "bus"]

Which of the following would turn vehicles into ["boat", "car", "bus", "plane"]?

appendItem(vehicles, "plane");
insertItem(vehicles, 2, "plane");
removeItem(vehicles, 0);
insertItem(vehicles, 1, "plane");

User Elvio
by
7.2k points

1 Answer

4 votes

Answer:

appendItem(vehicles, "plane");

Step-by-step explanation:

1. appendItem(list, "item") adds the "item" at the end of the list. In the case of appendItem(vehicles, "plane");, it adds "plane" at the end of the vehicles array.

appendItem(vehicles, "plane");

vehicles = ["boat", "car", "bus", "plane"]

2. insertItem(list, index, "item") adds the "item" at the specified index in the list. In the case of insertItem(vehicles, 2, "plane");, it adds "plane" to the second index of vehicles, moving all of the items to the right of "plane" one index higher.

insertItem(vehicles, 2, "plane");

vehicles = ["boat", "car", "plane", "bus"]

Note: it looks like the language you're using here is JavaScript, which uses a 0-based index. This means that, unlike languages that use 1-based index, it sets the first item to an index of 0 instead of 1. So in JS, an item with an index of 0 is the 1st item in an array, an index of 2 is the 3rd item in an array, etc. Pseudocode uses a 1-based index, and JS uses a 0-based index, so don't get confused with which language uses what index base! Therefore, vehicles would get vehiles ← ["boat", "plane", "car", "bus"] in pseudocode.

3. removeItem(list, index) wouldn't add anything to the array, but rather get rid of an item at the specified index, so this is definitely not helpful in adding "plane".

removeItem(vehicles, 0);

vehicles = ["car", "bus"]

4. insertItem(vehicles, 1, "plane")

vehicles = ["boat", "plane", "car", "bus"]

User GRoutar
by
8.0k points