DEV Community

Discussion on: Creating 3 Stacks With 1 Array in JavaScript

Collapse
 
geompse profile image
Geompse

Well, to me, using indexes is cheating because it is the same as using objects. Since you can access the [topIndex] value you access the [topIndex-1] value : that is in opposition with the concept of stacks (LIFO).

You might as well have objects with named properties (i.e. multipleStacks.stack1.value1) and no array at all.

For the sole purpose of science I would have enjoyed an "only one stack" version where you have to unstack in a temporary stack untill reaching the desired value to access or update (loop of Array.pop then loop of Array.push).

For the sake of fulfilling the use case, for production usage, I would have made one object containing an array per stack (but a multimensional array is cheating as well) and not one object containing two objects.

TL;DR it seems very bad not to use Array.pop nor Array.push in a JavaScript exercice regarding stacks.

Thread Thread
 
haraldlang profile image
Harald Lang

Well, I do not understand that complain.

The task was to implement a stack using an array, rather than to implement a stack using a stack.

So from my understanding, using indexed access is perfectly fine.