Created
June 8, 2023 13:00
-
-
Save ejke/143f41cf06649afc8b689f3b1a397f2d to your computer and use it in GitHub Desktop.
Refactoring game - how many changes would you make?
This file contains 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
import {readFileSync} from "fs"; | |
const plays = JSON.parse(readFileSync("../shared/plays.json")); | |
const invoices = JSON.parse(readFileSync("../shared/invoices.json")); | |
function statement(invoice, plays) {e | |
let totalAmount = 0; | |
let volumeCredits = 0; | |
let result = `Statement for ${ | |
invoice.customer | |
}\n`; | |
const format = new Intl.NumberFormat("en-US", { | |
style: "currency", | |
currency: "USD", | |
minimumFractionDigits: 2 | |
}).format; | |
for (let perf of invoice.performances) { | |
const play = plays[perf.playID]; | |
let thisAmount = 0; | |
switch (play.type) { | |
case "tragedy": thisAmount = 40000; | |
if (perf.audience > 30) { | |
thisAmount += 1000 * (perf.audience - 30); | |
} | |
break; | |
case "comedy": thisAmount = 30000; | |
if (perf.audience > 20) { | |
thisAmount += 10000 + 500 * (perf.audience - 20); | |
} | |
thisAmount += 300 * perf.audience; | |
break; | |
default: | |
throw new Error(`unknown type: ${ | |
play.type | |
}`); | |
} | |
// add volume credits | |
volumeCredits += Math.max(perf.audience - 30, 0); | |
// add extra credit for every ten comedy attendees | |
if ("comedy" === play.type) | |
volumeCredits += Math.floor(perf.audience / 5); | |
// print line for this order | |
result += ` ${ | |
play.name | |
}: ${ | |
format(thisAmount / 100) | |
} (${ | |
perf.audience | |
} seats)\n`; | |
totalAmount += thisAmount; | |
} | |
result += `Amount owed is ${ | |
format(totalAmount / 100) | |
}\n`; | |
result += `You earned ${volumeCredits} credits\n`; | |
return result; | |
} | |
console.log(statement(invoices, plays)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment