mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Notable changes: * Use informative variable names instead of q, a, b, m, and c * Use a lookup table instead of chained ifs for face cards * Refactor I/O helpers into a separate file * Refactor random card logic into its own function * Support any case for "yes" input * Add a few comments to non-obvious parts of the game logic * Insert "HERE IS THE CARD WE DREW:" into the output since just printing the card was pretty confusing * Make indentation uniform * Make bracing style uniform * Use conventional JavaScript camelCasing * Use top-level await in a module instead of a main() function * Clean up the HTML shell page for modern best practices (including a mobile-friendly viewport)
30 lines
609 B
JavaScript
30 lines
609 B
JavaScript
const outputEl = document.querySelector("#output");
|
|
|
|
export function print(string) {
|
|
outputEl.append(string);
|
|
}
|
|
|
|
export function readLine() {
|
|
return new Promise(resolve => {
|
|
const inputEl = document.createElement("input");
|
|
outputEl.append(inputEl);
|
|
inputEl.focus();
|
|
|
|
inputEl.addEventListener("keydown", event => {
|
|
if (event.key === "Enter") {
|
|
const result = inputEl.value;
|
|
inputEl.remove();
|
|
|
|
print(result);
|
|
print("\n");
|
|
|
|
resolve(result);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export function spaces(numberOfSpaces) {
|
|
return " ".repeat(numberOfSpaces);
|
|
}
|