This is the documentation into the Create your own calculator task
Supported operations are
num_1 + num_2
- Add two numbersnum_1 - num_2
- Subtract two numbersnum_1 * num_2
- Multiply two numbersnum_1 / num_2
- Divide two numbers
Operations can be stacked on top of each other, however operators cannot (++
is not allowed).
+ num_2
- Number is unchangednum_1 - num_2
- Number is negated
Unary operators are helpful to catch the first condition when user wants to have a negative number
There are several approaches to solve this problem. They depend on the implementation of server-client interface logic. You can either do
- Sequence (chunks) calculation
- Per character calculation
Having this in mind, we have three strategies
- Use external library to handle the state logic on the backend
- Create a stack based implementation given task specification
- Create a finite automata implementation with given restrictions
For this project I have chosen finite automata implementation for the following reasons
- It is enough to use for given current specification
- It provides enough of the challenge for the given time
- It is easily extendable with PDA solutions (stack based)
Given perfect color combination of orange and dark blue, taking the inspiration from iPhone calculator, this is the link to view design made in Figma: https://www.figma.com/design/5K6rzE1e0tKDiZkmG0anSP/Calculator?node-id=0-1&t=ZhYL6IyvHFVuV9Ig-1
Project is monorepo with the following structure
/backend
- Backend express server with typescript support. Runs usingnodemon
node.js application and its endpoint/frontend
- Frontend react application using vite as a bundler/types
- common Typescript definitions which could be used both on frontend and backend/docs
- files related to the documentation
Here I have described using state-transitions the way how logic part will work. Some things have changed (like the idea of the error, and introduction of one way down backward button), however the central idea is the same.

Our state is simple object
type State = {
// current incomming message from the frontend
incomingInput: string;
// current operator, or function which will run over the input
operation?: ValueOf<BinaryOperatorFx>,
/** tuple of two latest numbers.
* first is the concat result over last sequences of operations
* second is the second arg. for the operation
*/
numbers: [string, string],
// current result of the operation
result: number,
// represents error state, incicates for the app that something went wrong
error: boolean,
// current state function which control behavior of the program
fx: StateFx;
// previous state function which control behavior of the program
prevFx: StateFx;
}
State transitions happen when you meet a certain condition on state.fx
function. State transitions change the behavior of the program by moving its state.fx
to the next step according to the FA.
- Open root directory from terminal
- Go to the
/backend
and runnpm run dev
- Open another terminal and go to the
/frontend
directory and runnpm run dev
- Navigate to
https://localhost:5173/