票务售卖Kata测试未返回预期结果求助
Hey there! I see you've got this ticket change kata working in Ruby but hit a snag with your JavaScript implementation—let's fix that. First, let's recap the core requirement to make sure we're on the same page:
Kata需求:电影票单价25美元,需为顾客数组处理找零,若能为所有顾客找零返回"YES",否则返回"NO"。
Core Logic Breakdown
The key here is tracking the number of 25$ and 50$ bills we have on hand (100$ bills are useless for giving change, so we don't need to count them). Here's the rule set we need to follow for each customer:
- If a customer pays with 25$: No change needed—just increment our 25$ count.
- If a customer pays with 50$: We need to give back one 25$. If we don't have any, return "NO" immediately. Otherwise, decrement 25$ count and increment 50$ count.
- If a customer pays with 100$: Always prioritize giving back one 50$ + one 25$ first (this preserves more 25$ bills for future 50$ payments). If we don't have that combo, check if we have three 25$ bills. If neither works, return "NO".
Common Pitfalls to Check in Your Code
Chances are your bug is one of these:
- Trying to use 100$ bills for change (we never do this—only 25$ and 50$ are valid for giving back)
- Handling 100$ payments by using three 25$ bills first (this can leave you short on 25$s for subsequent 50$ customers)
- Incorrectly updating your bill count variables (e.g., forgetting to decrement, or incrementing the wrong one)
- Missing edge cases in your test suite (like a customer paying 50$ as the first person, or a 100$ payment after only two 25$s)
Working JavaScript Implementation (ticketClark.js)
Here's a clean, tested version of the function:
function tickets(peopleInLine) { let count25 = 0; let count50 = 0; for (const bill of peopleInLine) { switch (bill) { case 25: count25++; break; case 50: if (count25 < 1) return "NO"; count25--; count50++; break; case 100: // Prioritize 50+25 to save 25s for future 50s if (count50 >= 1 && count25 >= 1) { count50--; count25--; } else if (count25 >= 3) { count25 -= 3; } else { return "NO"; } break; default: return "NO"; // Invalid bill amount } } return "YES"; } module.exports = tickets;
Test Cases (test.js)
Use these to validate your code (works with Jest or any testing framework):
const tickets = require('./ticketClark'); // Successful scenarios test('Can handle multiple 25s and a 100', () => { expect(tickets([25, 25, 25, 100])).toBe("YES"); }); test('Can handle 25, 25, 50, 100 sequence', () => { expect(tickets([25, 25, 50, 100])).toBe("YES"); }); // Failed scenarios test('Cannot give change for 100 with only one 25', () => { expect(tickets([25, 100])).toBe("NO"); }); test('Cannot give change for first customer paying 50', () => { expect(tickets([50])).toBe("NO"); }); test('Runs out of 25s after two 100s', () => { expect(tickets([25, 25, 25, 100, 100])).toBe("NO"); });
How to Debug Your Existing Code
Compare your code against this implementation:
- Check if you're initializing
count25andcount50to 0 (not undefined or some other value) - Verify the order of operations for 100$ payments (50+25 first, then three 25s)
- Make sure you're returning "NO" as soon as you can't give change (don't let the loop continue)
内容的提问来源于stack exchange,提问作者pyan

