const DENSITY_G_PER_CUP = { all_purpose_flour: 120, granulated_sugar: 200, butter: 227, water: 236.588, white_rice_uncooked: 185 }; module.exports = { slug: "cups-to-grams-common-ingredients", title: "Cups to grams for common ingredients", deck_category: "cooking", description: "Converts a volume in US cups to a weight in grams for a small set of common cooking ingredients using cited density values.", inputs: [ { name: "ingredient", type: "string", required: true, description: "Ingredient to convert", enum: ["all_purpose_flour", "granulated_sugar", "butter", "water", "white_rice_uncooked"] }, { name: "cups", type: "number", unit: "US cups", required: true, description: "Volume in US cups", min: 0 } ], outputs: [ { name: "grams", unit: "g", description: "Equivalent weight in grams" } ], steps: [ "Look up the grams-per-cup density for the chosen ingredient from the cited table.", "grams = cups * grams_per_cup.", "Round the result to the nearest whole gram." ], constants: [ { name: "all_purpose_flour_g_per_cup", value: 120, unit: "g/cup", source: 1 }, { name: "granulated_sugar_g_per_cup", value: 200, unit: "g/cup", source: 0 }, { name: "butter_g_per_cup", value: 227, unit: "g/cup", source: 0 }, { name: "water_g_per_cup", value: 236.588, unit: "g/cup", source: 0 }, { name: "white_rice_uncooked_g_per_cup", value: 185, unit: "g/cup", source: 0 } ], sources: [ { kind: "url", value: "https://fdc.nal.usda.gov/", title: "FoodData Central, household measure weights for sugar, butter, water (1 US cup = 236.588 mL, density approx. 1 g/mL), and uncooked white rice", publisher: "USDA Agricultural Research Service", retrieved_utc: "2026-09-30T20:10:00Z", note: "Source for the sugar, butter, water, and rice grams-per-cup figures." }, { kind: "url", value: "https://www.kingarthurbaking.com/learn/ingredient-weight-chart", title: "Ingredient Weight Chart (all-purpose flour: 1 cup = 120 g, dip-and-sweep method)", publisher: "King Arthur Baking Company", retrieved_utc: "2026-09-30T20:10:00Z", note: "Source for the all-purpose flour grams-per-cup figure used by professional and home bakers." } ], disclaimer: null, questionText(inputs) { const name = inputs.ingredient.replace(/_/g, " "); return `How many grams are in ${inputs.cups} cups of ${name}?`; }, run(inputs) { const { ingredient, cups } = inputs; const density = DENSITY_G_PER_CUP[ingredient]; if (density === undefined) throw new Error("ingredient must be one of: " + Object.keys(DENSITY_G_PER_CUP).join(", ")); if (typeof cups !== "number" || cups < 0) throw new Error("cups must be a non-negative number"); const gramsRaw = cups * density; const grams = Math.round(gramsRaw); return { answer: `${grams} g`, values: { grams } }; }, examples: [ { name: "worked example", inputs: { ingredient: "all_purpose_flour", cups: 2 }, expected: { answer: "240 g" }, check: "2 * 120 = 240." }, { name: "test vector: water", inputs: { ingredient: "water", cups: 1.5 }, expected: { answer: "355 g" }, check: "1.5 * 236.588 = 354.882, rounds to 355." } ] };