Traveling Shopper - FreeCodeCamp #134 Daily Challenge

2 min

πŸ›’ Problem Summary

Given an initial amount and a list of items to buy (both in the format ["Amount", "Currency"]), how many can you buy in order, using the following conversion table?

CurrencyEquivalent in USD
USD1.00 USD
EUR1.10 USD
GBP1.25 USD
JPY0.0070 USD
CAD0.75 USD
  • If you can buy all, return β€œBuy them all!”
  • Otherwise, return β€œBuy the first x items” (x = number of items you can buy in order).

πŸ”Ž Analysis & Strategy

The key is to convert everything to USD before comparing. This avoids errors and simplifies the calculation. The process is:

  1. Convert the initial amount to USD.
  2. Iterate through the item list, converting each price to USD.
  3. Simulate sequential purchases: if you can afford it, subtract; if not, stop and return how many you could buy.

Why not convert to the original currency? Because all rates are given relative to USD, and converting everything to a single currency is more direct and less error-prone.


πŸ§ͺ Key Test Cases

  • The amount is enough for some but not all items β†’ should return the maximum possible.
  • The amount is exactly enough for all β†’ β€œBuy them all!”
  • Amount and items in mixed currencies β†’ must convert correctly.
  • Less common currencies (JPY, CAD) β†’ robustness.
  • Not enough for even the first item β†’ β€œBuy the first 0 items.”
  • Money left after buying everything β†’ still, β€œBuy them all!”

πŸ’‘ Commented Implementation

const rates = {
  USD: 1.0,
  EUR: 1.1,
  GBP: 1.25,
  JPY: 0.007,
  CAD: 0.75,
}

export default function buyItems(amount, items) {
  // Convert initial amount to USD
  let budget = Number.parseFloat(amount[0]) * rates[amount[1]]
  let count = 0

  for (const [price, currency] of items) {
    const priceUSD = Number.parseFloat(price) * rates[currency]
    if (budget >= priceUSD) {
      budget -= priceUSD
      count++
    }
    else {
      return `Buy the first ${count} items.`
    }
  }
  return 'Buy them all!'
}

⏱️ Complexity

  • Time: O(n)O(n) (single pass through the list)
  • Space: O(1)O(1) (no structures proportional to input)

⚠️ Edge Cases & Tips

  • Not enough for even the first item β†’ β€œBuy the first 0 items.”
  • Exactly enough or money left over β†’ β€œBuy them all!”
  • Always convert to USD before comparing.
  • Use parseFloat to handle decimals and integers.

πŸ€” Reflections & Learnings

  • Array destructuring.
  • Currency conversion with a fixed table.
  • Sequential greedy simulation.
  • Simple, clear, and optimal code for this case.

πŸ“š Resources