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

  1. calculateInvoice([{ unitPrice: 100, quantity: 2 }, { unitPrice: 50, quantity: -1 }], { country: 'IN' }): what is the total?
  2. Same items, but country: 'FR': what is the total, and why?
  3. Same items, country: 'IN', coupon { type: 'percent', value: 10, minSubtotal: 500 }: what is the discount?
  4. Same items, country: 'IN', coupon { type: 'fixed', value: 300 }: what are the discount and total?
  5. 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 };

Your submission

Do the work in your own editor with any AI tool, then paste it here. Resubmit as often as you like. Your submission is sent to OpenAI to write the review, so leave out passwords and API keys.

Number them 1 to 5. Give the exact values.

Paste your main prompts, or describe what you asked and what you checked yourself. The review comments on this too.