Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test completion suggestion for calculator (Challenge 01/03) #9

Open
wim-va opened this issue Sep 21, 2023 · 0 comments
Open

Test completion suggestion for calculator (Challenge 01/03) #9

wim-va opened this issue Sep 21, 2023 · 0 comments

Comments

@wim-va
Copy link

wim-va commented Sep 21, 2023

The test as-is didn't cover faulty operator handling, so I came up with

// calculator.test.js
// Division
...
// Error case: Invalid operator
  expect(() => calculator(num1, num2, "//")).toThrow("Invalid operator");

Adding the error throwing handling naturally requires the main file to throw an error in this case. I used the default for that:

function calculator(num1, num2, operator) {
    switch (operator) {
        case "+": return num1 + num2;
        case "-": return num1 - num2;
        case "*": return num1 * num2;
        case "/": return num1 / num2;
        default: throw new Error('Invalid operator');
    }
}

This way, anything other than the operators throws an error. You're welcome to use it, if you choose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant