Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push a number onto the stack.
DSA Javascript
stack.push([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Pushing the operator instead of the number.
Using the stack variable incorrectly.
✗ Incorrect
In postfix evaluation, when the token is a number, we push it onto the stack.
2fill in blank
mediumComplete the code to pop the top two numbers from the stack for operation.
DSA Javascript
const b = stack.pop(); const a = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop().
Using peek() which does not remove the element.
✗ Incorrect
We pop the top two numbers from the stack to apply the operator.
3fill in blank
hardFix the error in the switch case to correctly perform addition.
DSA Javascript
switch(operator) { case '+': result = a [1] b; break; }Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+'.
Using '*' or '/' which perform different operations.
✗ Incorrect
The '+' operator adds two numbers in JavaScript.
4fill in blank
hardFill both blanks to check if the token is an operator.
DSA Javascript
if (token [1] operators) { const operator = token; const b = stack.pop(); const a = stack.pop(); const result = applyOperator(a, b, operator); stack.[2](result); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'in' for membership check.
Using 'pop' instead of 'push' to add result.
✗ Incorrect
The 'in' keyword checks if token is in operators; 'push' adds result back to stack.
5fill in blank
hardFill all three blanks to complete the applyOperator function.
DSA Javascript
function applyOperator(a, b, operator) { switch(operator) { case '+': return a [1] b; case '-': return a [2] b; case '*': return a [3] b; default: throw new Error('Invalid operator'); } }Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators in cases.
Using division '/' instead of multiplication '*'.
✗ Incorrect
The function returns the result of applying the operator to a and b.
