Practice Task

Practice: Explain and triage a provided code snippet

Explain a JavaScript order-total function, identify likely bugs, and propose a minimal safe fix.

Practice typeRealistic client scenario · reviewed credit reward
SubmissionsOpen practice; every contributor can submit their own version
Review outcomePass: +25 Trust Points and +75 withdrawable Circle Credits. Revision or not passed: no points or credits until fixed.
Current statusOpen
Client Scenario

What problem are you solving?

A client has an old order-total function. They say zero-quantity gifts and orders with missing quantities are often wrong, but they have not asked you to rewrite production code.

System Context

Use these assumptions

Assume the code could run in Node.js or browser JavaScript. Do not access a repository and do not rewrite the whole module. Focus on explanation, risks, test cases, and minimal repair.

Source Material

Base your work only on this material

function total(items){
  let sum=0;
  for(const item of items){
    if(item.qty) sum += item.price * item.qty;
  }
  return sum.toFixed(2);
}

Client note: zero-quantity gifts and missing-quantity orders are often wrong.
Deliverables

What to submit

  • What the code currently does
  • Likely bugs and trigger conditions
  • At least five test cases
  • Minimal safe fix proposal
  • What not to change without more context
Review Criteria

What makes a result pass review

  • Mention qty=0 being skipped
  • Mention price/qty type risk
  • Mention toFixed returns a string
  • Do not suggest a full rewrite without evidence
  • Tie each test case to a risk
Submission Format

Recommended format

  • Structured engineering note
  • Small fix snippet is allowed, but explanation and tests are required