///
function startGame() {
// Starting a new game.
let playerName: string | undefined = getInputValue('playername');
logPlayer(playerName);
postScore(100, playerName);
}
function getInputValue(elementId: string): string | undefined {
// Type assertions applies to the whole document.get...., not to document.
// We know that we're only going to pass valid IDs, so will never get a null,
// so we can assert the result will always be an input element.
const inputElement: HTMLInputElement = document.getElementById(elementId);
if (inputElement.value === '') {
return undefined;
}
else {
return inputElement.value;
}
}
function postScore(score: number, playerName: string = 'MultiMath player'): void {
let logger: (value: string) => void;
if (score < 0) {
logger = logError;
}
else {
logger = logMessage;
}
let scoreElement: HTMLElement | null = document.getElementById('postedScores');
scoreElement!.innerText = `${score} - ${playerName}`;
logger(`Score: ${score}`);
}
function logPlayer(name: string = 'MultiMath player'): void {
console.log(`New game starting for player: ${name}.`);
}
function arm(doc: HTMLDocument) {
// We know getElementById() cannot be null because we control the markup, so
// we can use a non-null type assertion on the function call result.
doc.getElementById('startGame')!.addEventListener('click', startGame);
}
arm(document);
const logMessage = (message: string) => console.log(message);
function logError(message: string): void {
console.error(message);
}
let firstPlayer: Player = new Player();
firstPlayer.name = 'Laney';
console.log(firstPlayer.formatName());