File tree Expand file tree Collapse file tree 2 files changed +89
-2
lines changed Expand file tree Collapse file tree 2 files changed +89
-2
lines changed Original file line number Diff line number Diff line change 16
16
Once you've implemented the logic, test your code by running
17
17
*/
18
18
19
- class Calculator { }
20
-
19
+ class Calculator {
20
+ constructor ( ) {
21
+ this . result = 0 ;
22
+ }
23
+
24
+ add ( number ) {
25
+ if ( typeof number !== 'number' ) {
26
+ throw new Error ( 'Invalid input. Please provide a number.' ) ;
27
+ }
28
+ this . result += number ;
29
+ }
30
+
31
+ subtract ( number ) {
32
+ if ( typeof number !== 'number' ) {
33
+ throw new Error ( 'Invalid input. Please provide a number.' ) ;
34
+ }
35
+ this . result -= number ;
36
+ }
37
+
38
+ multiply ( number ) {
39
+ if ( typeof number !== 'number' ) {
40
+ throw new Error ( 'Invalid input. Please provide a number.' ) ;
41
+ }
42
+ this . result *= number ;
43
+ }
44
+
45
+ divide ( number ) {
46
+ if ( typeof number !== 'number' ) {
47
+ throw new Error ( 'Invalid input. Please provide a number.' ) ;
48
+ }
49
+ if ( number === 0 ) {
50
+ throw new Error ( Error ) ;
51
+ }
52
+ this . result /= number ;
53
+ }
54
+
55
+ clear ( ) {
56
+ this . result = 0 ;
57
+ }
58
+
59
+ getResult ( ) {
60
+ return this . result ;
61
+ }
62
+
63
+ calculate ( expression ) {
64
+ const sanitizedExpression = expression . replace ( / \s + / g, '' ) ; // Remove all spaces
65
+ const result = eval ( sanitizedExpression ) ; // Evaluate the expression
66
+ if ( isNaN ( result ) ) {
67
+ throw new Error ( 'Invalid expression. Please provide a valid arithmetic expression.' ) ;
68
+ }
69
+ this . result = result ;
70
+ }
71
+ }
72
+
73
+ // FILEPATH: /c:/Users/abrar/OneDrive/Desktop/100xDevops/assignments/01-js/hard/calculator.js
21
74
module . exports = Calculator ;
Original file line number Diff line number Diff line change 11
11
*/
12
12
13
13
class Todo {
14
+ constructor ( ) {
15
+ this . todos = [ ] ;
16
+ }
17
+
18
+ add ( str ) {
19
+ this . todos . push ( str ) ;
20
+ }
21
+ remove ( indexOfTodo ) {
22
+ this . todos . splice ( indexOfTodo , 1 ) ;
23
+ }
24
+ update ( index , updatedTodo ) {
25
+ if ( index < this . todos . length ) {
26
+ this . todos [ index ] = updatedTodo ;
27
+
28
+ }
29
+ }
30
+ getAll ( ) {
31
+ return this . todos ;
32
+ }
33
+
34
+ get ( indexOfTodo ) {
35
+ if ( indexOfTodo < this . todos . length ) {
36
+ return this . todos [ indexOfTodo ] ;
37
+ }
38
+ else return null ;
39
+
40
+ }
41
+
42
+ clear ( ) {
43
+ this . todos = [ ]
44
+ }
45
+
46
+
47
+
14
48
15
49
}
16
50
You can’t perform that action at this time.
0 commit comments