How I worked through my most recent JS project!

projectnick
3 min readOct 9, 2022

Here is an outline on how I built the JS for a calculator that I ran on Nodejs. I’ll be going over how I worked through the project.

(keep in mind, I downloaded Nodejs onto my computer, downloaded a readline-sync package off of NPM, ran it in the terminal, and connected it to the JS file in my IDE)

Step 1: Identify what you need to accomplish

The first thing I do before any project is make a running list of things I want to see happen. I gathered the requirements from my project and got to work…

  • ask user “what operation would you like to perform”
  • intake a symbol (/ , *, + ., -)
  • IF they don’t input an operator, print “This is not a valid operation” then re-run the program
  • IF yes, ask user to “please enter the first number”
  • gather first inputted number
  • IF it’s not a number, print “This is not a number” then re-ask
  • repeat process for second number
  • calculate the result
  • print “ the result is ‘x’”

Step 2: create variables and functions that will need to be used

After I planned out what I needed to accomplish this project, I then went through and made the basic variables and functions, along with what they should do and be used for:

  • getOperator — this variable will store the function that should: prompt question, receive correct operator and store it in a variable, deny and prompt user to put a valid operation if they put anything other than an operator sign.
  • getNum — this variable will store the function that should: prompt user to input number, store number in variable, or deny and prompt user to input a valid number if they inputted anything other than a number
  • calc — this variable will store the function that should: take the operator and the numbers inputted to calculate the result, it should be able to run through the four available operators and print the result
  • getOperator(); — call function
  • num1 = Number(getNum(‘first’)); — call function and input ‘type’ to change the prompt to first
  • num2 = Number(getNum(‘second’)); — call function and input ‘type’ to change the prompt to second

Step 3: Application

Now that I have mostly everything planned out… it’s time to put it all together! The rest wasn’t too bad after I discovered what I needed to do and how to do it. I got stuck on a couple parts and ended up using some arrow functions instead of regular functions. (Side note, I also downloaded a readline-sync package off of NPM).

Once I finished this, I went into my terminal and with the help of Nodejs, was able to have a little application that asked for my operator, the two numbers I wanted to use, and then printed the result!

My final code: https://codepen.io/nickjhoang/pen/XWqybVm

Keep in mind, it will not work if you copy and paste it into your IDE. You’ll need to download Nodejs onto your laptop/computer, go into your terminal, activate Nodejs, follow the instructions on NPM on how to download the package, input “ var rs = require(‘readline-sync’); “ at the top of your JS file, and BAM… it should work then.

Regardless if you are going through and building this exact project, I feel like this is a cool method to work and break down a project.

Until next time! (feedback is appreciated!)

--

--

projectnick

Software Developer || Creative || EX-Tech Recruiter/AM