Intuition versus rote logic - Queues lesson conundrum

When I read the instructions for the first step of this exercise, I figured all the necessary logic would be done with and we would go on. The remaining instructions disagreed with my notion but I went ahead anyway…

  dequeue() {
    if (this.isEmpty()) {
      throw Error('Queue is empty!')
    }
    const data = this.queue.removeHead();
    this.size--;
    console.log(`Removed ${data} from queue! Queue size is now ${this.size}.`);
    return data;
  }

We get to the next lesson/exercise and see what they were expecting in the last one. Now I am not objecting to anything. Intuitively I came up with my own solution from the first instruction and tweaked the outcome (at first I just returned false.) on subsequent instruction steps. The end result is what my mind saw at the outset. Is it not likely that many other minds have the same intuition? Could we not just use that from the get go? We don’t have to move any code, only insert the if statement, which is where I thought the exercise was going.

My solution was accepted but this was the starting code for the next exercise…

  dequeue() {
    if (!this.isEmpty()) {
      const data = this.queue.removeHead();
      this.size--;
      console.log(`Removed ${data} from queue! Queue size is now ${this.size}.`);
      return data;
    } else {
      throw new Error("Queue is empty!");
    }
  }

Bottom line, wrapping large blocks of code in if…else if…else code blocks is easy to avoid. Direct traffic, not real estate. I like my intuition over this one. Is it possible this particular lesson might get a looking over, based on my scant evidence/hypothesis?

Apologies to public users, only Pro subscribers can access this page,

https://www.codecademy.com/paths/pass-the-technical-interview-with-javascript/tracks/javascript-linear-data-structures/modules/queues-js/lessons/learn-queues-javascript/exercises/queue-conditions-avoiding-underflow

@catower, any thoughts?

1 Like

Hey I’m terribly sorry its taken me so long to get back to this. Had the page saved for nearly 2 weeks.

Based on what I’m reading, your suggestion is that the solution code is overly complicated, and could be improved by re-arranging the if statement?

1 Like