You're going to create a slightly unorthodox calculator in a TDD (test driven development) way.
Your calculator needs 3 functions - add(a, b), multiply(a, b) and factorial(x). When done, you may want to extend your calculator with more functionality.
For now, your calculator will be usable only on the CLI, but you think you may want to make it more flexible in the future, with a web-based interface. You also think it'll be so cool that others will want to use it in their programs too. As a result, you've decided to build tests for the core functions, and have the calculator as a standalone file that can be imported by either a CLI program or a web-based program.
Here are the 3 functions your calculator (calculator.js) needs to implement, and which also need thorough tests (in calculator.test.js):
add(a, b) positivelyWhat it says on the tin. However... the thought of negative numbers terrifies you. Therefore, the result must always be flipped to positive. You should expect(add(2, -5)).toBe(3) rather than the scary -3.
multiply(a, b) imperfectlyPretty basic, right? Take 2 inputs, and multiply them. However... you really hate the number 6 (any number calling itself perfect isn't to be trusted), so any output that should have the digit 6 in it needs to be censored and the whole answer replaced with 2 (your favourite number, being the first prime). So for example, you should expect(multiply(2, 13)).toBe(2) rather than 26, as the 6 in the output means the whole answer needs to be replaced with just 2.
factorial(x) with limitsNothing special here, except you can't imagine why anyone would ever want to find a number larger than 20!, so if x > 20 you can expect() the returned value to always be 'Too big!'.
In addition, your main.js program should ask the user to choose which of add, multiply and factorial they would like to do, then prompt them for the inputs to that function, before showing the output. It should then prompt them to try another calculation.
Welcome to the calculator!
Choose (1) for add, (2) for multiply or (3) for factorial
1, 2 or 3? 1
Choose the 1st number to add: 5
Choose the 2nd number to add: 8
5 + 8 is equal to 13
Choose (1) for add, (2) for multiply or (3) for factorial
1, 2 or 3? 2
Choose the 1st number to multiply: 2
Choose the 2nd number to multiply: 3
2 * 3 is equal to 2
Choose (1) for add, (2) for multiply or (3) for factorial
1, 2 or 3? 3
What number do you want the factorial of? 6
6! is 720
Choose (1) for add, (2) for multiply or (3) for factorial
1, 2 or 3?
In a terminal, create the folder with mkdir ~/code/calculator and move into it with cd ~/code/calculator. For this task you'll need 3 files, one for the core calculator functionality (calculator.js), one for the tests (calculator.test.js) and one for the main CLI program (main.js). You can create all 3 at once with:
# Linux, WSL or MacOS
touch calculator.js calculator.test.js main.js
# Windows PowerShell
ni calculator.js, calculator.test.js, main.js
Ideally, you should start by writing tests in calculator.test.js. I would suggest tackling one of your calculator's functions at a time, building tests for that function then writing that function and seeing if the tests pass, before moving on to the next calculator function. You may want to do just one test at a time, or all the tests for a function in one go.
You may find it useful to 'stub out' your calculator.js file first with the following:
export function add (a, b) {
return true
}
export function multiply (a, b) {
return true
}
export function factorial (x) {
return true
}
Only begin work on main.js once you've got a range of tests passing for your 3 calculator funcions.
Make sure you've thoroughly read through this lesson's guide on testing and modules first.
Ask your teacher to check your code.