Slide 68
Slide 68 text
describe(calculate.name, () => {
it('handles "1 + 1"', () => {
expect(calculate('1 + 1')).toEqual(2);
});
it('handles "2 - 1"', () => {
expect(calculate('2 - 1')).toEqual(1);
});
it('handles "2 * 2"', () => {
expect(calculate('2 * 2')).toEqual(4);
});
});
const OPERATIONS = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
};
export default function calculate(memory) {
const [a, operand, b] = memory.split(' ');
const operation = OPERATIONS[operand];
return operation(Number(a), Number(b));
}
Code Test
✔