Project Requirements:
The project requirement for TypeScript word count project is, the user will enter a English paragraph and all that is needed is to just to implement counting characters and words without whitespaces.
Analysis:
Implementation of TypeScript word count project is, a function that counts the words without whitespaces in an English paragraph.
Shebang code:
The line #!/usr/bin/env node
in TypeScript code is called a shebang. It tells the operating system to use the Node.js interpreter to run the script. This is a common convention in Node.js scripts.
Import modules:
The `inquirer` module is imported from the `inquirer` package. You need to install the inquirer module first using the npm install inquirer command.
Function startLoop:
Prompts the user to continue the loop.
Steps:
- Await the result of the
getSentence()
function. - Prompt the user to continue the loop using the
inquirer.prompt()
function. - Repeat steps 1 and 2 while the user selects “Yes”.
Function getSentence:
Prompts the user to enter a sentence and prints the word count.
Steps:
- Prompt the user to enter a sentence using the
inquirer.prompt()
function. - Trim the whitespace from the user’s input.
- Count the words in the user’s input using the
wordCount()
function. - Print the word count to the console.
Function wordCount:
Counts the number of words in a string.
Steps:
- Split the string into words using the
split()
method. - Log the words to the console for debugging purposes (optional).
- Return the length of the array of words.