Skip to content

Commit

Permalink
added both solve power function and both tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeljavan committed Mar 11, 2019
1 parent 1ffe136 commit 868c5d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/mathString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const input: string[] = [
'1*2/4*6+4*3',
'1-+-+--+2',
'1+++2----3--+-+4',
'1+2^3+2^(4^2+7)',
];
const faultInput = [
'1**1*1***2',
Expand Down Expand Up @@ -70,6 +71,7 @@ describe('solve phrases math', () => {
expect(solve(input[8])).toBe('15');
expect(solve(input[5])).toBe('-3');
expect(solve(input[4])).toBe('2');
expect(solve(input[11])).toBe('8388617');
});
});
describe('detect fault input', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/regulaExpersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ export const mathSigns = {
multiplication: '*',
divisin: '/',
summation: '+',
subtraction: '-'
subtraction: '-',
power: '^',
};
const numRegString: string = '[+-]?\\d*\\.?\\d+(e[+-]?\\d+)?';
export const numberRegExp: RegExp = new RegExp(`${numRegString}`, 'g');
export const betweenParenthesesRegExp: RegExp = new RegExp(
`\\(${numRegString}([\\${mathSigns.summation}\\${mathSigns.subtraction}\\${
mathSigns.multiplication
}\\${mathSigns.divisin}]${numRegString})*\\)`,
}\\${mathSigns.divisin}\\${mathSigns.power}]${numRegString})*\\)`,
'g'
);
export const divisionRegExp: RegExp = new RegExp(
Expand All @@ -26,3 +27,7 @@ export const summationOrSubtractionRegExp: RegExp = new RegExp(
}]${numRegString}`,
'g'
);
export const powerRegExp: RegExp = new RegExp(
`${numRegString}\\^${numRegString}`,
'g'
);
4 changes: 4 additions & 0 deletions src/selectModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
divisionRegExp,
multiplicationRegExp,
numberRegExp,
powerRegExp,
summationOrSubtractionRegExp
} from './regulaExpersions';
import { removeSpacesFromInput } from './utility';
Expand All @@ -26,3 +27,6 @@ export const selectDivisionPharse = (input: string) => {
export const selectSummationOrSubtractionPhrase = (input: string) => {
return input.match(summationOrSubtractionRegExp);
};
export const selectPowerPhrase = (input: string) => {
return input.match(powerRegExp);
};
22 changes: 21 additions & 1 deletion src/solveModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
selectMultiplicationPhrase,
selectNumbers,
selectParantheses,
selectPowerPhrase,
selectSummationOrSubtractionPhrase
} from './selectModules';
import { convertSpecialChar } from './utility';
Expand All @@ -22,7 +23,8 @@ export const solveBetweenParantheses = (input: string): string => {
};
export const solveMathPhrases = (phrase: string) => {
let result = solveSevralPositiveAndNegativeSings(phrase);
result = solveDivisionPharse(phrase);
result = solvePowerPhrase(result);
result = solveDivisionPharse(result);
result = solveMultiplicationPhrase(result);
result = solveSummationOrSubtractionPhrase(result);
result = result.replace(/[()]/g, '');
Expand Down Expand Up @@ -106,3 +108,21 @@ export const solveSevralPositiveAndNegativeSings = (phrase: string): string => {
}
return phrase;
};
export const solvePowerPhrase = (phrase: string): string => {
const powerPhrase = selectPowerPhrase(phrase);
if (!powerPhrase) {
return phrase;
}
powerPhrase.forEach(phr => {
const _result = solvePower(phr);
phrase = phrase.replace(phr, _result);
});
return solvePowerPhrase(phrase);
};
export const solvePower = (phr: string): string => {
const numbers = selectNumbers(phr)!;
const num1 = +numbers[0];
const num2 = +numbers[1];
const result = Math.pow(num1, num2);
return result > 0 ? `+${result}` : result.toString();
};

0 comments on commit 868c5d4

Please sign in to comment.