Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA Javascript
stack.[1](element);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() removes an element instead of adding.
Using shift() or unshift() modifies the start of the array, not the end.
✗ Incorrect
The push() method adds an element to the top of the stack (array).
2fill in blank
mediumComplete the code to check if the stack is empty.
DSA Javascript
if (stack.[1] === 0) { return true; } else { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using size or count which are not valid properties for JavaScript arrays.
Using empty which is not a property.
✗ Incorrect
The length property gives the number of elements in an array (stack).
3fill in blank
hardFix the error in popping the top element from the stack.
DSA Javascript
let top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using push() adds an element instead of removing.
peek() and top() are not standard JavaScript array methods.
✗ Incorrect
pop() removes and returns the last element from the stack.
4fill in blank
hardFill both blanks to correctly check operator precedence.
DSA Javascript
function precedence(op) {
if (op === '+' || op === '-') return [1];
if (op === '*' || op === '/') return [2];
return 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the same precedence to all operators.
Confusing precedence values.
✗ Incorrect
Addition and subtraction have precedence 1; multiplication and division have precedence 2.
5fill in blank
hardFill all three blanks to correctly convert infix to postfix for an operator.
DSA Javascript
while (stack.length > 0 && precedence(stack[stack.length - 1]) [1] precedence(token)) { output.push(stack.[2]()); } stack.[3](token);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes wrong order of operators.
Using push() instead of pop() to remove elements.
Forgetting to push the current token after popping.
✗ Incorrect
We pop from stack while top has higher or equal precedence, then push the current token.
