Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
  <meta charset="utf-8">
  <title>JavaScript program to Remove elements from a stack that do not meet a condition</title>
</head>
<body>

</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                class Stack {
  constructor() {
    this.items = [];
  }
  // push element to the stack
  push(element) {
    this.items.push(element);
  }

  // pop element from the stack
  pop() {
    if (this.items.length == 0) {
      return "Underflow";
    }
    return this.items.pop();
  }

  // get the top element of the stack
  peek() {
    return this.items[this.items.length - 1];
  }

  // check if the stack is empty
  isEmpty() {
    return this.items.length == 0;
  }

  // get the size of the stack
  size() {
    return this.items.length;
  }

  filter(callback) {
    let result_stack = new Stack();
    for (let i = 0; i < this.items.length; i++) {
      if (callback(this.items[i])) {
        result_stack.push(this.items[i]);
      }
    }
    return result_stack;
  }
  
 displayStack(stack) {
  console.log("Stack elements are:");
  let str = "";
  for (let i = 0; i < stack.items.length; i++)
    str += stack.items[i] + " ";
  return str.trim();
 }
}

const stack1 = new Stack();
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.push(4);
stack1.push(5);
stack1.push(6);
stack1.push(7);
stack1.push(8);
console.log("Original Stack:");
console.log(stack1.displayStack(stack1));
console.log("Create new stack, values greater than 5:");
let result = stack1.filter((element) => element > 5);
console.log(result.displayStack(result));
console.log("Create new stack, values less than 5:");
result = stack1.filter((element) => element < 5);
console.log(result.displayStack(result));
console.log("Create new stack, only even numbers:");
result = stack1.filter((element) => element % 2 === 0);
console.log(result.displayStack(result));
console.log("Create new stack, only odd numbers:");
result = stack1.filter((element) => element % 2 !== 0);
console.log(result.displayStack(result));

              
            
!
999px

Console