Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the top element from the stack.
DSA Javascript
function pop(stack) {
if (stack.length === 0) return null;
return stack.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() will add an element instead of removing.
Using shift() or unshift() removes or adds elements at the start, not the end.
✗ Incorrect
The pop() method removes the last element added to the stack, which is the top element.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA Javascript
function safePop(stack) {
if (stack.[1] === 0) return null;
return stack.pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using size or count which are not valid properties for arrays in JavaScript.
Using length() as a function instead of a property.
✗ Incorrect
The length property gives the number of elements in the array (stack).
3fill in blank
hardFix the error in the pop function to avoid popping from an empty stack.
DSA Javascript
function popElement(stack) {
if (stack.length === 0) {
return [1];
}
return stack.pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning undefined can be confusing as it may mean variable not set.
Returning 0 or false might be mistaken for valid stack elements.
✗ Incorrect
Returning null clearly indicates no element was popped because the stack was empty.
4fill in blank
hardFill both blanks to pop the top element and store it in a variable.
DSA Javascript
function popAndStore(stack) {
const topElement = stack.[1]();
return [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() will add an element instead of removing.
Returning stack instead of the popped element.
✗ Incorrect
pop() removes the top element, which is stored in topElement and then returned.
5fill in blank
hardFill all three blanks to pop an element only if the stack is not empty, else return null.
DSA Javascript
function conditionalPop(stack) {
if (stack.[1] === 0) {
return [2];
} else {
return stack.[3]();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using shift() instead of pop() changes stack behavior.
Returning undefined or 0 instead of null for empty stack.
✗ Incorrect
Check stack length, return null if empty, else pop the top element.
