PROJECT REQUIREMENTS:

Develop a simple command line calculator using TypeScipt, Node.js and Inquirer. Create a GitHub repository for the project and submit its URL and NPX command in the project submission form. Please note that you will create a NPX command for your project and publish it on npm.

TypeScript CLI Calculator Project code explained step by step:

Shebang:

The line #!/usr/bin/env node in TypeScript code is called a shebang. It is a special line that tells the operating system how to interpret the file. In this case, the shebang tells the operating system to use the Node.js interpreter to run the file.

The shebang is a single line that starts with a hash (#) character, followed by the path to the interpreter. In this case, the path is /usr/bin/env node. The env part of the path tells the operating system to look for the Node.js interpreter in the environment variables.

The shebang is a common practice in Unix-like operating systems, such as Linux and macOS. It is not necessary to use a shebang in TypeScript code, but it is a good practice to do so if you want to make the file executable.

Import Modules:

import inquirer from "inquirer";

import chalk from "chalk";

import chalkAnimation from "chalk-animation";

Imports three modules from NPM:

  • inquirer: This module provides a library for creating interactive command-line prompts.
  • chalk: This module provides a library for styling text in the terminal.
  • chalk-animation: This module provides a library for animating text in the terminal.

The first line, import inquirer from "inquirer";, imports the inquirer module and assigns it to the variable inquirer. The second line, import chalk from "chalk";, imports the chalk module and assigns it to the variable chalk. The third line, import chalkAnimation from "chalk-animation";, imports the chalk-animation module and assigns it to the variable chalkAnimation.

These modules can be used to create interactive command-line applications that are more user-friendly and engaging. For example, the inquirer module can be used to create prompts for users to enter information, and the chalk module can be used to style the text of the prompts and the output of the application. The chalk-animation module can be used to animate the text of the prompts and the output of the application.

Sleep Function:

Defines a function called sleep. This function takes no arguments and returns a Promise. The Promise will be resolved after 2000 milliseconds (2 seconds).

The function first creates a new Promise object. The Promise object has a resolve method and a reject method. The resolve method is called when the Promise is resolved, and the reject method is called when the Promise is rejected.

The function then calls the setTimeout function to schedule a callback function to be executed after 2000 milliseconds. The callback function will call the resolve method on the Promise object.

The sleep function can be used to pause the execution of the program for a specified amount of time.

Welcome Function:

defines an async function called welcome. This function takes no arguments and returns nothing.

The function first creates three objects using the chalkAnimation module:

  • rainbowTitle: This object will display the text “Lets start command line calculator” in rainbow colors.
  • pulseTitle: This object will display the text “Developed by AHMER ARMAN for TypeScript faculty test.” in a pulsing effect.
  • neonTitle: This object will display the text of a calculator keyboard in neon colors.

The function then calls the sleep function to pause the execution of the program for a specified amount of time. The sleep function is called three times, once for each object.

Finally, the function calls the startLoop function to start the main loop of the calculator.

The welcome function is used to welcome the user to the calculator and to display the calculator keyboard. The function uses the chalkAnimation module to create animated and stylish text effects.

Ask Question Function:

Defines an async function called askQuestion. This function takes no arguments and returns nothing.

The function first uses the inquirer module to prompt the user to select an operation from a list of options:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division

The function then uses the type property to specify that the answer should be a string. The name property specifies the name of the variable that will be assigned to the user’s answer.

The function then prompts the user to enter two numbers. The numbers are assigned to the variables num1 and num2.

Finally, the function uses a switch statement to determine which operation to perform. The switch statement compares the value of the operator variable to the values of the four options. If the value of the operator variable matches one of the options, the corresponding code is executed. If the value of the operator variable does not match any of the options, the default code is executed.

The askQuestion function is used to get the user’s input and to perform the corresponding operation. The function uses the inquirer module to make the prompts more user-friendly.

The JavaScript code is slightly different from the TypeScript code because it uses the async and await keywords. These keywords are used to handle asynchronous code in JavaScript. However, the two codes are functionally equivalent.

Start Loop Function:

Defines an async function called startLoop. This function takes no arguments and returns nothing.

The function first uses a do-while loop to repeatedly execute the following steps:

  1. Call the askQuestion function to get the user’s input and perform the corresponding operation.
  2. Use the inquirer module to prompt the user to answer the question “Do you want to continue?” with the choices “Yes” and “No”.
  3. Assign the user’s answer to the variable again.

The loop continues as long as the value of the restart variable is equal to “Yes”.

The startLoop function is used to create a loop that allows the user to repeatedly perform calculations. The function uses the inquirer module to make the prompts more user-friendly.