ThePrimeagen / kata-machine

1.28k stars 1.27k forks source link

What about this implementation of pop() for Stack? #73

Open SolKuczala opened 7 months ago

SolKuczala commented 7 months ago

I'm following your course (thanks for it, it's helping me a lot), and I'm following the implementations along and, while doing the pop() method for the Stack class, I got stuck with the proposed implementation and, I came up with this implementation instead, that passed your tests:

pop(): T | undefined {
        if (!this.head) {
            return undefined;
        }
        this.length--
        const temp = this.head
        this.head = this.head.prev

        return temp.value
    }

What would be the difference with yours? Am I missing something? Thank you :)

EnzoKACI commented 7 months ago

had a similar issue, though your code did not work for me actually rolled back to version seen in the proposed implementation then found the issue actually came from the pushing part where I was missing the correct node.prev udpate as the tests are not independant (ie they push elements then check if they popped out in the right order) you might get undefined wrongly for elements that should have been defined in the previous tests