ran npm run tdd & generated TDD
This commit is contained in:
28
test/solveRPN/solveRPN.js
Normal file
28
test/solveRPN/solveRPN.js
Normal file
@ -0,0 +1,28 @@
|
||||
module.exports = rpn => {
|
||||
const OPERATORS = {
|
||||
'*': (a, b) => a * b,
|
||||
'+': (a, b) => a + b,
|
||||
'-': (a, b) => a - b,
|
||||
'/': (a, b) => a / b,
|
||||
'**': (a, b) => a ** b
|
||||
};
|
||||
const [stack, solve] = [
|
||||
[],
|
||||
rpn
|
||||
.replace(/\^/g, '**')
|
||||
.split(/\s+/g)
|
||||
.filter(el => !/\s+/.test(el) && el !== '')
|
||||
];
|
||||
solve.forEach(symbol => {
|
||||
if (!isNaN(parseFloat(symbol)) && isFinite(symbol)) {
|
||||
stack.push(symbol);
|
||||
} else if (Object.keys(OPERATORS).includes(symbol)) {
|
||||
const [a, b] = [stack.pop(), stack.pop()];
|
||||
stack.push(OPERATORS[symbol](parseFloat(b), parseFloat(a)));
|
||||
} else {
|
||||
throw `${symbol} is not a recognized symbol`;
|
||||
}
|
||||
});
|
||||
if (stack.length === 1) return stack.pop();
|
||||
else throw `${rpn} is not a proper RPN. Please check it and try again`;
|
||||
};
|
||||
13
test/solveRPN/solveRPN.test.js
Normal file
13
test/solveRPN/solveRPN.test.js
Normal file
@ -0,0 +1,13 @@
|
||||
const test = require('tape');
|
||||
const solveRPN = require('./solveRPN.js');
|
||||
|
||||
test('Testing solveRPN', (t) => {
|
||||
//For more information on all the methods supported by tape
|
||||
//Please go to https://github.com/substack/tape
|
||||
t.true(typeof solveRPN === 'function', 'solveRPN is a Function');
|
||||
//t.deepEqual(solveRPN(args..), 'Expected');
|
||||
//t.equal(solveRPN(args..), 'Expected');
|
||||
//t.false(solveRPN(args..), 'Expected');
|
||||
//t.throws(solveRPN(args..), 'Expected');
|
||||
t.end();
|
||||
});
|
||||
Reference in New Issue
Block a user