Search code examples
javascriptalgorithmstack

I am stuck with a JavaScript algorithm attempting to find the max value of a stack


I am working on a JavaScript algorithm attempting to go thru the stack and** return -Infinity when the stack is empty** and when the stack is not empty return the max value inside of this stack. They require me to use 3 different methods

.pop() which pops the top value of the stack

.push() which pushes a value on to the stack

.peek() which shows me the top value of the stack without modifying the stack

Below is what I tried which works for all test cases except for when the stack is full of negative values, as well as the code that was provided to me for the class Stack, if I am missing information in any way please let me know and I will update the question to try and make it clearer.

this is what I tried thus far it works for all of the test cases except for when the stack is Stack(-847626, -9234752, -7444), when this happens it returns 0 rather than the greater value of this stack screenshot of the error

function maxValue(stack) {

  let count = 0;
  while (true) { // while stack is a truthy value
    if (stack.pop() === undefined) {//keep popping from stack until undefined
      return -Infinity

    }
    if(stack.pop()>count){
      count = stack.pop()
    }
    return count
    break
  }



}

This is the class methods for the stack that where provided to me

class Stack {

  constructor(...values) {

    const data = {};

    let index = 0;

    this.push = function (value) {
      if (arguments.length < 1) {
        throw new TypeError('stack.push() requires a value argument');
      }
      if (typeof value === 'undefined') {
        throw new TypeError('stack.push(value) received undefined');
      }
      data[index] = value;
      index++;
    };

    this.pop = function () {
      const last = index - 1;
      if (last < 0) return;
      const value = data[last];
      delete data[last];
      index = last;
      return value;
    };

    this.peek = function () {
      const last = index - 1;
      if (last < 0) return;
      return data[last];
    };

    this.print = function () {
      if (index === 0) {
        return 'Stack { <empty> }';
      }
      let output = ' }';
      let last = index - 1;
      for (; last > 0; last--) {
        output = ' <- ' + JSON.stringify(data[last]) + output;
      }
      output = JSON.stringify(data[last]) + output;
      return 'Stack { ' + output;
    };

    for (let i = 0; i < values.length; i++) {
      this.push(values[i]);
    }

    Object.freeze(this);

  }

}

Solution

  • You're forgetting that pop() removes the top element from the stack. You're calling it three times before actually updating the max value, essentially just throwing two elements in the garbage each iteration.

    class Stack {
    
      constructor(...values) {
    
        const data = {};
    
        let index = 0;
    
        this.push = function (value) {
          if (arguments.length < 1) {
            throw new TypeError('stack.push() requires a value argument');
          }
          if (typeof value === 'undefined') {
            throw new TypeError('stack.push(value) received undefined');
          }
          data[index] = value;
          index++;
        };
    
        this.pop = function () {
          const last = index - 1;
          if (last < 0) return;
          const value = data[last];
          delete data[last];
          index = last;
          return value;
        };
    
        this.peek = function () {
          const last = index - 1;
          if (last < 0) return;
          return data[last];
        };
    
        this.print = function () {
          if (index === 0) {
            return 'Stack { <empty> }';
          }
          let output = ' }';
          let last = index - 1;
          for (; last > 0; last--) {
            output = ' <- ' + JSON.stringify(data[last]) + output;
          }
          output = JSON.stringify(data[last]) + output;
          return 'Stack { ' + output;
        };
    
        for (let i = 0; i < values.length; i++) {
          this.push(values[i]);
        }
    
        Object.freeze(this);
    
      }
    
    }
    
    function maxValue(stack) {
      let max = -Infinity;
      while (stack.peek() !== undefined) {
        const next = stack.pop();
        if (next > max) max = next;
      }
      return max;
    }
    
    console.log(maxValue(new Stack(-847626, -9234752, -7444)));