Understand code you didn’t write
- Skill
- Reading code with AI
- Time
- About 20 min
- Access
- Free
You’ve just joined a team, and your first ticket touches the billing code below. Nobody who wrote it is around.
AI is great at explaining code, and sometimes confidently wrong about it. Your job is to understand this function well enough to answer exact questions, and to check the AI’s answers by running the code rather than trusting them.
Answer these 5 questions
calculateInvoice([{ unitPrice: 100, quantity: 2 }, { unitPrice: 50, quantity: -1 }], { country: 'IN' }): what is thetotal?- Same items, but
country: 'FR': what is thetotal, and why? - Same items,
country: 'IN', coupon{ type: 'percent', value: 10, minSubtotal: 500 }: what is thediscount? - Same items,
country: 'IN', coupon{ type: 'fixed', value: 300 }: what are thediscountandtotal? - Name one behaviour a product manager would probably call a bug, and show an input that triggers it.
Submit
Your 5 answers, with the actual numbers. Then a line on how you used AI and what you verified yourself.
invoice.js
// invoice.js: used by the billing serviceconst TAX_RATES = { IN: 0.18, DE: 0.19, US: 0 }; function calculateInvoice(items, { country = 'US', coupon = null } = {}) { let subtotal = 0; for (const item of items) { if (item.quantity <= 0) continue; subtotal += item.unitPrice * item.quantity; } let discount = 0; if (coupon?.type === 'percent') { discount = subtotal * (coupon.value / 100); } else if (coupon?.type === 'fixed') { discount = Math.min(coupon.value, subtotal); } if (coupon?.minSubtotal && subtotal < coupon.minSubtotal) { discount = 0; } const taxable = subtotal - discount; const taxRate = TAX_RATES[country] ?? 0.2; const tax = Math.round(taxable * taxRate * 100) / 100; return { subtotal, discount, tax, total: Math.round((taxable + tax) * 100) / 100, };} module.exports = { calculateInvoice };