Skip to content

Instantly share code, notes, and snippets.

View WesleySmits's full-sized avatar

Wesley Smits WesleySmits

View GitHub Profile
npm login
@WesleySmits
WesleySmits / perform-calculation.ts
Created October 16, 2021 09:00
Simple web calculator: Perform calculation
#performCalculation(): void {
const displayQuery = this.query.replace('÷', '/').replace('x', '*');
const answer: number = eval(displayQuery);
const formattedAnswer = +parseFloat(answer.toString()).toFixed(2);
this.query = formattedAnswer.toString();
}
@WesleySmits
WesleySmits / call-updatedisplay.diff
Created October 16, 2021 08:59
Simple web calculator: Call update display from query setter method
public set query(value: string) {
this.#query = value;
+ this.#updateDisplay();
}
@WesleySmits
WesleySmits / update-display.ts
Created October 16, 2021 08:58
Simple web calculator: Update display method
#updateDisplay(): void {
if (this.#display === null) {
return;
}
this.#display.innerText = this.query;
}
@WesleySmits
WesleySmits / perform-calculation.ts
Created October 16, 2021 08:58
Simple web calculator: perform-calculation method
#performCalculation(): void {
}
@WesleySmits
WesleySmits / handle-button-click.ts.diff
Created October 16, 2021 08:57
Simple web calculator: Added special button event handling
#handleButtonClick(event: Event): void {
const target = event.target as HTMLButtonElement;
if (target === null) {
return;
}
const { value } = target;
const userValue = target.innerText;
+ switch (value) {
@WesleySmits
WesleySmits / handle-button-click.ts
Created October 16, 2021 08:55
Simple web calculator: Handle button click method
#handleButtonClick(event: Event): void {
const target = event.target as HTMLButtonElement;
if (target === null) {
return;
}
const { value } = target;
const userValue = target.innerText;
}
@WesleySmits
WesleySmits / event-listeners.ts
Created October 16, 2021 08:54
Simple web calculator: Adding event bindings to calculator class
protected connectedCallback(): void {
this.#setEventListeners();
}
protected disconnectedCallback(): void {
this.#unsetEventListeners();
}
#setEventListeners(): void {
this.#buttons.forEach((button) => {
@WesleySmits
WesleySmits / calculator-properties-ts.diff
Created October 16, 2021 08:52
Simple web calculator: Adding properties to calculator class
class Calculator extends HTMLElement {
+ #display: HTMLElement | null = null;
+ #buttons: HTMLButtonElement[] = [];
+ #query = '';
+ public get query(): string {
+ return this.#query;
+ }
@WesleySmits
WesleySmits / main.pcss
Last active October 16, 2021 10:09
Simple web calculator: Styling for the calculator
body {
background: darkgray;
}
.calculator {
display: block;
height: 440px;
width: 300px;
margin-top: 10%;
margin-left: auto;