Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
/*
Implement a class `Calculator` having below methods
- initialise a result variable in the constructor and keep updating it after every arithmetic operation
- add: takes a number and adds it to the result
- subtract: takes a number and subtracts it from the result
- multiply: takes a number and multiply it to the result
- divide: takes a number and divide it to the result
- clear: makes the `result` variable to 0
- getResult: returns the value of `result` variable
- calculate: takes a string expression which can take multi-arithmetic operations and give its result
example input: `10 + 2 * ( 6 - (4 + 1) / 2) + 7`
Points to Note:
1. the input can have multiple continuous spaces, you're supposed to avoid them and parse the expression correctly
2. the input can have invalid non-numerical characters like `5 + abc`, you're supposed to throw error for such inputs

Once you've implemented the logic, test your code by running
- `npm run test-calculator`
*/
var math = require('mathjs');
class Calculator {
constructor(){
this.result=0;
}
add(number){
this.result+=number
}
subtract(number){
this.result-=number;
}
multiply(number){
this.result*=number;
}
divide(number){
if(number==0)
throw new Error("invalid number");
this.result/=number;
}
clear(){
this.result=0;
}
getResult(){
return this.result;
}
calculate(str){
var calculate =str.replace(/\s+/g,'');

if (/[^0-9+\-*/().\s]/.test(calculate)) {
throw new Error('non numeric characters in the expression');
}

const divisionByZeroPattern = /\/\s*0/g;
var boolean = divisionByZeroPattern.test(calculate);
if(boolean==true)
throw new Error("division by 0 not allowed");

else{
this.result = math.evaluate(calculate);
return this.result;
}
}


}

class Calculator {}

module.exports = Calculator;