Simple Calculator In JavaScript

RMAG news

Audience:

Anyone who can write this tutorial without seeing it.

Requirements:

os: linux
node.js

For Impatient people like me:

Link to repo

An Overview:

1.The program starts (Obviously!).
2.It asks the user to enter:
1) First Number.
2) Math Symbol(+,-,*,/,%).
3) Second Number.
3.According to the symbol entered by the user:
Operation is done on the two numbers and result is printed.
4.End

Steps:

1.Create Project Folder ‘Simple-Calculator-In-Js’.
2.Create main.js inside Project Folder.
3.At the beginning of main.js, past this comment:

/***********************************
* Simple Calculator In JavaScript *
***********************************/

Pal: Ah! Why?
Me: Just for feel!

4.Now create a function called main().
This will be the entry-point to our app.

function main(){
}

5.Now, call it just below the main().

main();

Ya! ya! ya! It does nothing!
Be patient, pal!

6.Now, install a module ‘readline-sync’ like this:

npm install readline-sync

Pal: Why do we need that!
Me: Just do what I say, cadet!
Do you need user input or not?

7.Import it into your code:

/***********************************
* Simple Calculator In JavaScript *
***********************************/

const readline = require(readline-sync);

function main(){
}
main();

8.Now lets fill the main() func!

9.First, print the name of the app:

main() {
console.log(`Simple Calculator (ctrl + c for quit!)`);
}

10.Start a loop for prompting the user again and again(after each operation):

main() {
console.log(`Simple Calculator (ctrl + c for quit!)`);

while(true) {
}
}

Disclaimer: If you just ran the code, then you may got your birthday present (infinite loop).
To break the loop, press: ctrl + c.

11.Take the three inputs:

while (true) {
//Take User Input
let num1 = Number(readline.question(`Enter first number:`));
let symbol = readline.question(`Enter symbol(+,-,*,/,%):`);
let num2 = Number(readline.question(`Enter second number:`));
}

Pal: That is a mouthful.
Me: Well, yep! But, lets break it down!

Explanation:
1)Ask the user for num1 using readline.question() and convert it into number by using Number() function and store it.
2)Ask the user for math symbol and store it.
3)Ask the user for num2 and convert it to number and store it!

12.Now, do the operation on the two numbers according to user’s entered symbol and print Result:

while (true) {
//Take User Input
let num1 = Number(readline.question(`Enter first number:`));
let symbol = readline.question(`Enter symbol(+,-,*,/,%):`);
let num2 = Number(readline.question(`Enter second number:`));

//Calculate According To User’s Choice
switch (symbol) {
case `+`:
console.log(`Result: ${num1 + num2}`);
break;
case `-`:
console.log(`Result: ${num1 num2}`);
break;
case `*`:
console.log(`Result: ${num1 * num2}`);
break;
case `/`:
console.log(`Result: ${num1 / num2}`);
break;
case `%`:
console.log(`Result: ${num1 % num2}`);
break;
default:
console.log(`Invalid symbol!`);
break;
}
}

Me:That’s it!
Pal: Wait! wait! wait!
What is the default thing doing?
Me:Yeah! Think, pal!
If user enters a non-math-symbol, then:
Yell at user that it is an invalid symbol.