Skip to content

Commit ff58ec5

Browse files
committed
updated calculator logic component
1 parent ecf6fd0 commit ff58ec5

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/app/calculator/logic/logic.component.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,69 @@ import { Component } from '@angular/core';
44
selector: 'app-logic',
55
imports: [],
66
templateUrl: './logic.component.html',
7-
styleUrl: './logic.component.css'
7+
styleUrl: './logic.component.css',
88
})
99
export class LogicComponent {
10+
displayedValue: string = '0';
11+
currentValue: number | null = null;
12+
operation: string | null = null;
13+
isNewInput: boolean = true;
14+
15+
onDigitClick(digit: string) {
16+
if (this.isNewInput) {
17+
this.displayedValue = digit;
18+
this.isNewInput = false;
19+
} else {
20+
this.displayedValue =
21+
this.displayedValue === '0' ? digit : this.displayedValue + digit;
22+
}
23+
}
24+
25+
calculate() {
26+
const currentNumber = parseInt(this.displayedValue);
27+
if (this.operation === '+') {
28+
this.currentValue! += currentNumber;
29+
} else if (this.operation === '-') {
30+
this.currentValue! -= currentNumber;
31+
} else if (this.operation === '*') {
32+
this.currentValue! *= currentNumber;
33+
} else if (this.operation === '/') {
34+
this.currentValue! /= currentNumber;
35+
}
36+
37+
this.displayedValue = this.currentValue!.toString();
38+
}
39+
40+
onOperationClick(op: string) {
41+
if (this.currentValue === null) {
42+
this.currentValue = parseInt(this.displayedValue);
43+
} else if (this.operation) {
44+
this.calculate();
45+
}
46+
47+
this.operation = op;
48+
this.isNewInput = true;
49+
}
50+
51+
onEqualsTo(){
52+
if(this.operation && this.currentValue !== null){
53+
this.calculate();
54+
this.currentValue = null;
55+
this.operation = null;
56+
}
57+
58+
}
59+
60+
onClear() {
61+
this.displayedValue = '0';
62+
this.operation = null;
63+
this.currentValue = null;
64+
this.isNewInput = true;
65+
}
66+
67+
onDisplayValueChange(newValue:string){
68+
this.displayedValue = newValue;
69+
}
70+
1071

1172
}

0 commit comments

Comments
 (0)