0

So I made a function to push user-inputted strings to a stack by using the built in .push(e) function like so:

push() {
    const arrays = this.array.push(String(this.userInput))
    console.log(this.array)

  }

and with every click of the push button the console updates the array pushing whatever the user has inputted into an HTML text field which I have also made. I showed it to a friend and they told me that this method was sort of cheating as I'm making a Stack of stacks and that there is a way to...

Implement a Stack using only an index, a count, and an array.

Conceptually I know what these are, the index is an objects position in a given array, and an array is a collection of objects of the same variable types and a count is ostensibly a count (correct me if I'm wrong?). However tying these concepts together to implement a stack is a little beyond me as a first semester computer science student is there a lay-mans terms way of explaining how these things can be tied together to implement a stack?

2
  • I'm making a Stack of stacks No, you still just have a single stack of strings. Not sure what the issue is with your code, are you saying that using push is forbidden, or what? Commented Dec 30, 2019 at 1:34
  • no no nothing is wrong it works just fine the way it is I'm more curious about the main part of the question which is how to implement this functionality only using an array, an index and a count aka not using the built-in .push() and conversely with the pop as well. and i was confused by that statement as well because the console log was just spitting out arrays with the user-inputted item in the next index position Commented Dec 30, 2019 at 1:36

1 Answer 1

1

To do what you're doing without using the built-in push method, just assign to the index at the current length of the array. No need to keep track of any other variables:

push() {
    this.array[this.array.length] = String(this.userInput);
    // if you also need your implementation to return the new length, then:
    return this.array.length;
}

Or, for pop:

pop() {
    const item = this.array[this.array.length - 1];
    this.array.length = Math.max(this.array.length - 1, 0);
    return item;
}

Keep in mind that push returns the new length of the array, so const arrays = this.array.push(String(this.userInput)) won't give you an array in return.

Sign up to request clarification or add additional context in comments.

5 Comments

aha ok so it returns the new length but it also updates the entire array in the background as well? Also, I tried the method above it works great with push but with pop it only seems to work once
That's what the built-in push method does. If you want to emulate that, then you'll have to both create a property on the array and return the new length separately.
for instance if i have an array with [4,5,6] when I pop the first time the console outputs [4,5, undefined] and then when i pop again it still get [4,5,undefined]
I thought that delete would update the array length, but I guess not. Reassign the array length instead
I think the delete is leaving the array length the same but just removing the data from the element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.