Skip to content

Commit 6bd220a

Browse files
author
Drago
committed
solution
1 parent 7e0d07f commit 6bd220a

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

01-js/hard/calculator.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@
1616
Once you've implemented the logic, test your code by running
1717
*/
1818

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
2174
module.exports = Calculator;

01-js/hard/todo-list.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@
1111
*/
1212

1313
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+
1448

1549
}
1650

0 commit comments

Comments
 (0)