Start learning free

LESSON 117 OF 671 · ARRAYS - CHALLENGES

Undo the Last Add in JavaScript: push and pop

Practice Read the full explanation

You keep a shopping list, an array of strings items. Add the word first to the end of the list, then add the word second to the end, then undo the last add by removing the last item. Return the list.

The undo always removes the item at the end of the list, even when the same word also appears earlier in the list.

 

Example 1:

Input: items = ["milk", "eggs"], first = "bread", second = "jam"
Output: ["milk", "eggs", "bread"]
Explanation: After the two adds the list is ["milk", "eggs", "bread", "jam"]. The undo removes "jam", the last item.

Example 2:

Input: items = [], first = "apples", second = "pears"
Output: ["apples"]
Explanation: The list starts empty. "apples" and "pears" are added, then "pears" is removed.

Example 3:

Input: items = ["water"], first = "juice", second = "water"
Output: ["water", "juice"]
Explanation: The list becomes ["water", "juice", "water"]. The undo removes the last item, the "water" just added, not the first one.

 

Constraints:

  • items has between 0 and 10 words: it may be empty.
  • first, second and every word in items are single words of lowercase English letters.
  • The same word may appear more than once.
STEP 1 OF 4
    Ctrl + Enter to check

    JAVASCRIPT · ARRAYS - CHALLENGES

    Undo The Last Add in JavaScript, explained

    Problem overview

    The method undoLastAdd receives a shopping list items (an array of strings) and two words, first and second. It adds first to the end of the list, adds second to the end, then undoes the last add by removing the last item. It returns the list.

    With items = ["milk", "eggs"], first = "bread" and second = "jam", the list becomes ["milk", "eggs", "bread", "jam"] and the undo removes "jam". The result is ["milk", "eggs", "bread"]. With an empty list, first = "apples" and second = "pears", the result is ["apples"].

    Approach: a list used as a stack

    Adding to the end and removing from the end are the two operations that arrays do best. push appends one or more values to the end of an array, and pop removes the last element and returns it. Used together, they make the array behave like a stack: the last thing added is the first thing removed. That is exactly what "undo the last add" means.

    The lesson needs no loop and no condition. It needs three calls in the right order.

    The same two methods work in the other direction at the front of an array: unshift adds at the start and shift removes the first element. Those are slower on long arrays, because every other element has to move over by one place, so the end of the array is the natural place to add and remove.

    Step by step

    1. Push first. It goes after every item already in the list. On an empty list it becomes the only item.
    2. Push second. It is now the last item, because it was added last.
    3. Pop. pop doesn't look at what the last item is. It removes the one at the end, which is second.
    4. Return items. The array was changed in place, and after the undo it ends with first.

    The solution in JavaScript

    class Solution {
        undoLastAdd(items, first, second) {
            items.push(first);
            items.push(second);
            items.pop();
            return items;
        }
    }
    

    Details worth knowing:

    • push and pop change the array itself. They don't build a new one. The function returns the same array object it received, so the caller's variable sees the changes too. After const list = ["a"] and const result = new Solution().undoLastAdd(list, "b", "c"), the expression result === list is true and list is ["a", "b"].
    • pop returns the removed element. This solution ignores the returned value, which is fine here because the lesson only needs the list.
    • Calling pop on an empty array returns undefined and leaves the array empty. That can't happen in this lesson, since two words have just been pushed.

    Edge cases and common mistakes

    • The same word earlier in the list. With items = ["water"], first = "juice" and second = "water", the list becomes ["water", "juice", "water"]. The undo removes the last "water", the one just added, and the result is ["water", "juice"]. Code that searches for second and removes the first match would delete the wrong item. pop never searches.
    • An empty starting list. No special case is needed: the two pushes work on an empty array, and the pop leaves first behind.
    • Swapping the pushes. If second is pushed before first, the pop removes first and the list ends with the wrong word.
    • Using shift instead of pop. shift removes the first element, not the last.

    Testing

    • items = ["milk", "eggs"], first = "bread", second = "jam" gives ["milk", "eggs", "bread"].
    • items = [], first = "apples", second = "pears" gives ["apples"].
    • items = ["water"], first = "juice", second = "water" gives ["water", "juice"].
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free