w3resource

JavaScript Stack - Exercises, Practice, Solution

JavaScript Stack [35 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a JavaScript program to implement a stack with push and pop operations. Find the top element of the stack and check if the stack is empty or not. Go to the editor

Click me to see the solution

2. Write a JavaScript program to sort the elements of a given stack in descending order. Go to the editor

Click me to see the solution

3. Write a JavaScript program to sort the elements of a given stack in ascending order. Go to the editor

Click me to see the solution

4. Write a JavaScript program to reverse the elements of a given stack. Go to the editor

Click me to see the solution

5. Write a JavaScript program to find the maximum and minimum elements in a stack. Go to the editor

Click me to see the solution

6. Write a JavaScript program to remove all the elements from a given stack. Go to the editor

Click me to see the solution

7. Write a JavaScript program to count all the elements in a given stack. Go to the editor

Click me to see the solution

8. Write a JavaScript program to implement a stack that checks if a given element is present or not in the stack. Go to the editor

Click me to see the solution

9. Write a JavaScript program to remove duplicates from a given stack. Go to the editor

Click me to see the solution

10. Write a JavaScript program to find the top and bottom elements of a given stack. Go to the editor

Click me to see the solution

11. Write a JavaScript program to rotate the stack elements to the left direction. Go to the editor

Click me to see the solution

12. Write a JavaScript program to rotate the stack elements to the right direction. Go to the editor

Click me to see the solution

13. Write a JavaScript program to get the middle element(s) of a given stack. Go to the editor

Click me to see the solution

14. Write a JavaScript program to remove a specific element from a stack. Go to the editor

Click me to see the solution

15. Write a JavaScript program to swap the top two elements of a given stack. Go to the editor

Click me to see the solution

16. Write a JavaScript program to get the nth element from the top of the stack. Go to the editor

Click me to see the solution

17. Write a JavaScript program to get the nth element from the bottom of the stack. Go to the editor

Click me to see the solution

18. Write a JavaScript program to implement a stack and move the nth element from the top of the stack to the top. Go to the editor

Click me to see the solution

19. Write a JavaScript program to implement a stack and move the nth element from the bottom of the stack to the top. Go to the editor

Click me to see the solution

20. Write a JavaScript program to merge two stacks into one. Go to the editor

Click me to see the solution

21. Write a JavaScript program to implement a stack using a linked list with push and pop operations. Find the top element of the stack and check if the stack is empty or not. Go to the editor

Click me to see the solution

22. Write a JavaScript program to implement a stack that supports toArray() operation, which converts the stack into an array. Go to the editor

Click me to see the solution

23. Write a JavaScript program that can create stacks from arrays. Go to the editor

Click me to see the solution

24. Write a JavaScript program to implement a stack that supports concat() operation, which concatenates two stacks into a new stack. Go to the editor

Click me to see the solution

25. Write a JavaScript program to implement a stack that creates a copy of the stack. Go to the editor

Click me to see the solution

26. Write a JavaScript program that implements a stack and checks if a stack is a subset of another stack. Go to the editor

Click me to see the solution

27. Write a JavaScript program that implements a stack and checks if two stacks are equal. Go to the editor

Click me to see the solution

28. Write a JavaScript program that implements a stack and finds elements that are common in two stacks.. Go to the editor

Click me to see the solution

29. Write a JavaScript program that implements a stack and find elements that are in the first stack but not in the second stack. Go to the editor

Click me to see the solution

30. Write a JavaScript program that implements a stack and creates a new stack that contains all elements from both stacks without duplicates. Go to the editor

Click me to see the solution

31. Write a JavaScript program that implements a stack and creates a new stack that contains only elements that are in either the first or the second stack, but not in both. Go to the editor

Click me to see the solution

32. Write a JavaScript program that implements a stack and creates a new stack from a portion of the original stack. Go to the editor

Click me to see the solution

33. Write a JavaScript program that implements a stack and checks if all elements of the stack satisfy a condition. Go to the editor

Click me to see the solution

34. Write a JavaScript program that implements a stack and checks if at least one element of the stack satisfies a condition. Go to the editor

Click me to see the solution

35. Write a JavaScript program that implements a stack and create a new stack by removing elements that do not satisfy a condition. Go to the editor

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Live Demo:

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.

JavaScript: Tips of the Day

Creates an array of elements, ungrouping the elements in an array produced by zip and applying the provided function

Example:

const tips_unzip = (arr, fn) =>
  arr
    .reduce(
      (acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
      Array.from({
        length: Math.max(...arr.map(x => x.length))
      }).map(x => [])
    )
    .map(val => fn(...val));

console.log(tips_unzip([[2, 15, 200], [3, 25, 300]], (...args) => args.reduce((acc, v) => acc + v, 0)));

Output:

[5, 40, 500]