A. Using assignment (=) instead of comparison (===) in if condition.
B. maxSize should be stack.length, not a constant.
C. Function should return stack.length instead of boolean.
D. Stack array should be empty to check if full.
Solution
Step 1: Identify the if condition mistake
The condition uses single equals = which assigns value instead of comparing.
Step 2: Correct comparison operator
It should use === to compare stack length with maxSize.
Final Answer:
Using assignment (=) instead of comparison (===) in if condition. -> Option A
Quick Check:
Use === for comparison, not = [OK]
Hint: Use === for comparison, not = [OK]
Common Mistakes:
Using = instead of ===
Confusing maxSize with stack length
Returning wrong type from function
5. You have a stack with max size 5. After pushing 3 elements, you want to check if the stack is full or empty and print the status as a string: "Full", "Empty", or "Partial". Which code correctly implements this?
hard
A. if (stack.length > 5) { console.log('Full'); } else if (stack.length < 0) { console.log('Empty'); } else { console.log('Partial'); }
B. if (stack.length === 5) { console.log('Full'); } else if (stack.length === 0) { console.log('Empty'); } else { console.log('Partial'); }
C. if (stack.length === 0) { console.log('Full'); } else if (stack.length === 5) { console.log('Empty'); } else { console.log('Partial'); }
D. if (stack.length >= 5) { console.log('Empty'); } else if (stack.length <= 0) { console.log('Full'); } else { console.log('Partial'); }
Solution
Step 1: Check conditions for full and empty stack
Full means length equals max size (5), empty means length zero.
Step 2: Use else for partial stack
If neither full nor empty, stack is partially filled.
Final Answer:
if (stack.length === 5) { console.log('Full'); } else if (stack.length === 0) { console.log('Empty'); } else { console.log('Partial'); } -> Option B
Quick Check:
Check length === maxSize for full, === 0 for empty [OK]
Hint: Check length === maxSize for full, === 0 for empty [OK]
Common Mistakes:
Using wrong comparison operators
Swapping full and empty conditions
Checking length > maxSize or < 0
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.