Project Requirements:

This TypeScript Number Guess Game is a short TypeScript/Node.js project that allows the user to guess the number generated by the computer. There are also several ways to alter the game, like adding more rounds or displaying the score. It’s quite simple and uses the random function to generate a number.

Analysis:

In the TypeScript Number Guess Game, the computer generates a number from 1 to 10 and tells the user whether the number is even or odd. The user then enters their guess. If the user guesses the correct number, the score increases by 1. The computer then asks the user if they want to continue playing.

Now we will discuss the code for the CLI number guessing game block by block.

Shebang code:

The #!/usr/bin/env node line tells the operating system to use the Node.js interpreter to run the script. This is a common convention in Node.js scripts.

The #!/usr/bin/env part tells the operating system where to find the Node.js interpreter. The node part is the name of the Node.js interpreter.

This line is typically placed at the beginning of a Node.js script. It is important to note that this line is only a convention and is not required.

Import modules:

The `inquirer` module is imported from the `inquirer` package.

The import statement is used to import modules into a TypeScript program. The inquirer module is a third-party module, which means that it is not part of the standard TypeScript library. To use the inquirer module, you need to install it first. You can do this using the npm install inquirer command.

Once the inquirer module is installed, you can import it into your TypeScript program using the import statement. The from keyword specifies the name of the package that the module belongs to. In this case, the module belongs to the inquirer package.

The inquirer module provides a variety of functions for creating interactive prompts for users. These functions can be used to collect input from users, such as their name, email address, or password. They can also be used to display messages to users, such as instructions or error messages.

Declaring variables:

The variable `score` is declared to be of type number and is assigned the value 0. The score variable can be used to store the score of a game.

Start Loop Function:

The async function startLoop() repeatedly prompts the user to guess a number until the user chooses not to continue.

The async keyword specifies that the function is asynchronous, which means that it can return a value after a delay or when an event occurs. The function keyword is used to define a function. The startLoop() function takes no arguments and returns no value.

The dowhile loop repeatedly executes the code block inside the loop as long as the condition is true. In this case, the condition is again.restart == "Yes", which means that the loop will continue as long as the user chooses to continue.

The await keyword is used to wait for the asynchronous operation to complete before continuing execution of the code. In this case, the asynchronous operation is the guessNumber() function and the inquirer.prompt() function.

Guess Number Function:

The function guessNumber() generates a random number between 1 and 10, prompts the user to guess the number, and displays the result.

The Math.floor() function is used to round down the value of a number. In this case, the value of Math.random() * 10 is rounded down to the nearest integer.

The ifelse statement checks if the value of the guessNum variable is even. If it is, the tip variable is assigned the value “Tip: Number is even.” Otherwise, the tip variable is assigned the value “Tip: Number is Odd.”.

The inquirer.prompt() function is used to create a prompt for the user to enter their guess. The prompt is created using the { type: "number", name: "userguess", message: "Guess a number between 1 to 10 (${tip}): " } object.

The console.log() function is used to print a message to the console. In this case, the message is the user’s guess and the system-generated number.

The ifelse statement checks if the user’s guess is correct. If it is, the score is incremented and a message congratulating the user is displayed. Otherwise, a message telling the user to try again is displayed.

Baca juga : Situs Judi Bola Online Terbaik 2024 Gacor dan Aman untuk Slot hanya di Playme8

Ahmer Arman

JII Store

Best Laptop

GitHub Link