Even though my JS code throws the correct error, it still fails. Here is the relevant Jest code block:
describe('operate', () => {
test("works with addition", () => {
expect(evaluate_expression("3+5")).toEqual(8);
});
test("works with substraction", () => {
expect(evaluate_expression("128-29")).toEqual(99);
});
test("works with multiplication", () => {
expect(evaluate_expression("25*5")).toEqual(125);
});
test("works with division", () => {
expect(evaluate_expression("990/99")).toEqual(10);
});
test("division 0 is handled", () => {
expect(evaluate_expression("5/0")).toThrow('Division by zero');
});
});
JavaScript code snippet:
function append_to_display(value) {
if (start == false)
document.getElementById('display').value += value;
else {
document.getElementById('display').value = value;
};
};
function calculate() {
try {
const expression = document.getElementById('display').value;
console.log(expression);
const result = evaluate_expression(expression);
document.getElementById('display').value = result;
document.getElementById('current_value').textContent = result;
} catch (error) {
document.getElementById('display').value = 'Error';
}
};
function evaluate_expression(expression) {
const output_queue = [];
const operator_stack = [];
const operators = { '+': 1, '-': 1, '*': 2, '/': 2 };
const tokens = expression.match(/([0-9]+|\+|\-|\*|\/)/g);
tokens.forEach(token => {
if (!isNaN(token)) {
output_queue.push(parseFloat(token));
} else if (token in operators) {
while (
operator_stack.length > 0 &&
operators[token] <= operators[operator_stack[operator_stack.length - 1]]
) {
output_queue.push(operator_stack.pop());
}
operator_stack.push(token);
} else {
throw new Error('Invalid expression');
}
});
while (operator_stack.length > 0) {
output_queue.push(operator_stack.pop());
}
const result_stack = [];
output_queue.forEach(token => {
if (!isNaN(token)) {
result_stack.push(token);
} else {
const b = result_stack.pop();
const a = result_stack.pop();
switch (token) {
case '+':
result_stack.push(a + b);
break;
case '-':
result_stack.push(a - b);
break;
case '*':
result_stack.push(a * b);
break;
case '/':
if (b === 0) {
throw new Error('Division by zero'); // HERE IS THE PROBLEM
}
result_stack.push(a / b);
break;
default:
throw new Error('Invalid operator');
}
}
});
if (result_stack.length !== 1) {
throw new Error('Invalid expression');
}
evaluate_expression()
is triggered by calculate()
when the "=" button is clicked on the calculator.
This is the specific line causing the issue in the JavaScript code:
if (b === 0) {
throw new Error('Division by zero'); // HERE IS THE PROBLEM
}
I have attempted to use rejects
with expect
here, but it has not worked as expected.