inventory.ts en TypeScript
Un script de inventario completo: entran registros tipados, sale un reporte agrupado y totalizado.
interface Item {
name: string;
quantity: number;
unitPrice: number;
}
const inventory: Item[] = [
{ name: "widget", quantity: 12, unitPrice: 2.5 },
{ name: "gadget", quantity: 3, unitPrice: 19.99 },
{ name: "doohickey", quantity: 40, unitPrice: 0.75 },
];
function totalValue(items: Item[]): number {
return items.reduce((sum, i) => sum + i.quantity * i.unitPrice, 0);
}
function lowStock(items: Item[], threshold = 5): Item[] {
return items.filter((i) => i.quantity < threshold);
}
console.log(`total value: $${totalValue(inventory).toFixed(2)}`);
for (const item of lowStock(inventory)) {
console.log(`low stock: ${item.name} (${item.quantity} left)`);
}
Palabras clave y builtins usados aquí
Itemconstforfunctioninterfacenumberofreturnstring
El intento, en números
- Líneas
- 24
- Caracteres a escribir
- 660
- Tokens
- 194
- Ritmo de tres estrellas
- 110 tpm
Al ritmo de tres estrellas de 110 tokens por minuto, este intento toma unos 106 segundos.
Paso 1 de 1 en Bis; paso 20 de 20 en Genéricos.