Created
January 13, 2026 02:35
-
-
Save davidystephenson/b74cdee3853bd0709af237cfbe35dcca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const departments = { | |
| engineering: { | |
| budget: 500000, | |
| headcount: 45, | |
| location: "Building A", | |
| }, | |
| marketing: { | |
| budget: 300000, | |
| headcount: 20, | |
| location: "Building B", | |
| }, | |
| }; | |
| const projects = { | |
| alphaRelease: { | |
| budget: 150000, | |
| duration: 6, | |
| priority: "high", | |
| }, | |
| betaPrototype: { | |
| budget: 80000, | |
| duration: 3, | |
| priority: "medium", | |
| }, | |
| }; | |
| export function extractMetric(data, key, metric, formatter) { | |
| const entry = data[key]; | |
| const value = entry[metric]; | |
| const formatted = formatter(value); | |
| return formatted; | |
| } | |
| function formatString (value) { | |
| return value.toUpperCase() | |
| } | |
| function formatNumber(value) { | |
| return value.toFixed(2) | |
| } | |
| const headcount = extractMetric(departments, "engineering", "headcount", formatNumber); | |
| console.log('engineering headcount:', headcount); | |
| const duration = extractMetric(projects, "alphaRelease", "priority", formatString); | |
| console.log('alphaRelease priority:', duration); | |
| // This version should throw a type error | |
| // const invalid = extractMetric(departments, "marketing", "budget", formatString); | |
| // console.log('marketing budget:', invalid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment