0% found this document useful (0 votes)
2 views3 pages

Caclulator TypeScript

The document outlines a simple calculator implementation in TypeScript, detailing the class structure and methods for appending numbers, clearing input, choosing operators, and computing results. It includes event listeners for button clicks to handle user interactions. The calculator supports basic arithmetic operations: addition, subtraction, multiplication, and division.

Uploaded by

Chandrini CK
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Caclulator TypeScript

The document outlines a simple calculator implementation in TypeScript, detailing the class structure and methods for appending numbers, clearing input, choosing operators, and computing results. It includes event listeners for button clicks to handle user interactions. The calculator supports basic arithmetic operations: addition, subtraction, multiplication, and division.

Uploaded by

Chandrini CK
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple Calculator – TypeScript Code

1. Basic appending of numbers alone:


type Operator = "+" | "-" | "*" | "/";
class Calculator {
private currentInput:
private previousInput: string = "";
private operator: Operator | null = null;
public appendNumber(num: string): void {
if ([Link] === "0") {
[Link] = num;
}
else {
[Link] += num;
}
[Link]();
}
private updateDisplay(): void {
const display = [Link]("display") as
HTMLInputElement;
[Link] = [Link];
}
}
const calculator = new Calculator();
document .getElementById("buttons")! .addEventListener("click",
(event) => {
const target = [Link] as HTMLElement;
if ([Link]("number")) {
[Link]([Link]);
}
});

2. Clearing the input field: Add the function in Calculator class


public clear():void{
[Link]=”0”;
[Link]();
}

const calculator = new Calculator();


[Link]("buttons")!.addEventListener("click",
(event) => { const target = [Link] as HTMLElement;
if ([Link]("number"))
[Link]([Link]);
else if([Link]("clear")){
[Link]();
})

3. Accepting operator inputs:

Add the function in Calculator Class


public chooseOperator(op: Operator): void {
if ([Link] === "")
return;
[Link] = op;
[Link] = [Link];
[Link] = "0";
[Link]();
}

const calculator = new Calculator();


[Link]("buttons")!.addEventListener("click",
(event) => { const target = [Link] as HTMLElement;
if ([Link]("number"))
[Link]([Link]);
else if([Link]("clear"))
[Link]();
else if ([Link]("operator"))
[Link]([Link] as Operator); });

4. Computation
Add the function in Calculator class
public compute(): void {
const prev = parseFloat([Link]);
const curr = parseFloat([Link]);
let result: number = 0;
if ([Link] === "+") result = prev + curr;
else if ([Link] === "-") result = prev - curr;
else if ([Link] === "*") result = prev * curr;
else if ([Link] === "/") result = prev / curr;
[Link] = String(result);
[Link] = "";
[Link] = null;
[Link]();
}

const calculator = new Calculator();


[Link]("buttons")!.addEventListener("click",
(event) => {
const target = [Link] as HTMLElement;
if ([Link]("number"))
[Link]([Link]);
else if([Link]("clear"))
[Link]();
else if([Link]("operator"))
[Link]([Link] as Operator);
else if([Link]("equals"))
[Link](); });

You might also like