Skip to content

Commit

Permalink
complete simple math string module
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeljavan committed Mar 7, 2019
1 parent d2ce282 commit af26f94
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/mathString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const input: string[] = [
'1*2/4*6+4*3'
];

xdescribe('test Regex for inputs', () => {
describe('test Regex for inputs', () => {
it('select numbers', () => {
expect(selectNumbers(input[0])).toEqual(['1', '+11', '+9', '-11e23']);
});
Expand All @@ -34,26 +34,26 @@ xdescribe('test Regex for inputs', () => {
]);
});
it('select multiple or divide two numbers', () => {
expect(selectMultiplicationPhrase(input[1])).toEqual(['1*1', '2/5']);
expect(selectMultiplicationPhrase(input[1])).toEqual(['1*1']);
});
});
describe('solve phrases math', () => {
xit('solve multiplication and return possitive', () => {
it('solve multiplication and return possitive', () => {
expect(solveMultiplication(input[2])).toBe('+9');
});
xit('solve multiplication and return negative', () => {
it('solve multiplication and return negative', () => {
expect(solveMultiplication(input[3])).toBe('-9');
});
xit('solve division', () => {
expect(solveDivisionPharse(input[4])).toBe('*2');
it('solve division', () => {
expect(solveDivisionPharse(input[4])).toBe('4*0.5');
});
xit('solve a phrase without parantheses', () => {
it('solve a phrase without parantheses', () => {
expect(solveMathPhrases(input[5])).toBe('-3');
});
it('solve a phrase with parantheses', () => {
expect(solve(input[7])).toBe('+97.5');
expect(solve(input[8])).toBe('+15');
expect(solve(input[7])).toBe('97.5');
expect(solve(input[8])).toBe('15');
expect(solve(input[5])).toBe('-3');
expect(solve(input[4])).toBe('+2');
expect(solve(input[4])).toBe('2');
});
});
7 changes: 5 additions & 2 deletions src/solveModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
selectParantheses,
selectSummationOrSubtractionPhrase
} from './selectModules';
import { converSpecialChar } from './utility';
import { convertSpecialChar } from './utility';

export const solveBetweenParantheses = (input: string): string => {
let result: string = input;
Expand Down Expand Up @@ -80,7 +80,10 @@ export const solveSummationOrSubtraction = (phrase: string) => {
const num1 = numbers[0];
const num2 = numbers[1];
const mathOperator = phrase.replace(
new RegExp(`(${converSpecialChar(num1)}|${converSpecialChar(num2)})`, 'g'),
new RegExp(
`(${convertSpecialChar(num1)}|${convertSpecialChar(num2)})`,
'g'
),
''
);
const result =
Expand Down
2 changes: 1 addition & 1 deletion src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const removeSpacesFromInput = (inp: string) => {
return inp.replace(/\s*/g, '');
};

export const converSpecialChar = (inp: string) => {
export const convertSpecialChar = (inp: string) => {
return `${inp
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')
Expand Down

0 comments on commit af26f94

Please sign in to comment.