From 7bea34f9f0430b96380f8cdf96eabe5dc5b2795f Mon Sep 17 00:00:00 2001 From: Hasnain Ahmed <156552529+HasnainSolangi@users.noreply.github.com> Date: Thu, 2 May 2024 01:25:40 +0500 Subject: [PATCH 1/2] Create cli-wordCounter --- cli-wordCounter | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 cli-wordCounter diff --git a/cli-wordCounter b/cli-wordCounter new file mode 100644 index 00000000..5bd9a5ab --- /dev/null +++ b/cli-wordCounter @@ -0,0 +1,63 @@ +import inquirer from "inquirer"; +import chalk from "chalk"; + + +// Function to count words and display the result +async function countWords(sentence: string) { + const words = sentence.trim().split(" "); + console.log(words); + console.log(chalk.blue(`Your sentence word count is ${words.length}`)); +} + +// Welcome message for the user +console.log(chalk.green("Welcome! Let's count the words in your sentence.")); + +// Prompt the user to enter a sentence +inquirer.prompt([ + { + name: "Sentence", + type: "input", + message: "Enter your sentence to count the words:" + } +]).then(async (answers: { Sentence: string }) => { + await countWords(answers.Sentence); + // Prompt for adding or removing words + while (true) { + const action = await inquirer.prompt([ + { + name: "action", + type: "list", + message: "Do you want to add or remove a word?", + choices: ["Add", "Remove", "Exit"] + } + ]); + if (action.action === "Add") { + const newWord = await inquirer.prompt([ + { + name: "word", + type: "input", + message: "Enter the word to add:" + } + ]); + answers.Sentence += " " + newWord.word; + await countWords(answers.Sentence); + } else if (action.action === "Remove") { + const wordToRemove = await inquirer.prompt([ + { + name: "word", + type: "input", + message: "Enter the word to remove:" + } + ]); + if (answers.Sentence.includes(wordToRemove.word)) { + answers.Sentence = answers.Sentence.replace(new RegExp(`\\b${wordToRemove.word}\\b`, "g"), "").replace(/\s{2,}/g, " ").trim(); + await countWords(answers.Sentence); + } else { + console.log(chalk.red("Sorry, the word you entered is not found in the sentence. Please enter a valid word to remove.")); + } + } else { + console.log(chalk.yellow("Thank you for using our word counter. See you soon!")); + process.exit(0); // Exit the process + } + } +}); From b2334104f0c907a70f106fa3fb028842d86b9186 Mon Sep 17 00:00:00 2001 From: Hasnain Ahmed <156552529+HasnainSolangi@users.noreply.github.com> Date: Thu, 2 May 2024 01:37:44 +0500 Subject: [PATCH 2/2] cli-wordCounter --- cli-wordCounter | 63 ------------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 cli-wordCounter diff --git a/cli-wordCounter b/cli-wordCounter deleted file mode 100644 index 5bd9a5ab..00000000 --- a/cli-wordCounter +++ /dev/null @@ -1,63 +0,0 @@ -import inquirer from "inquirer"; -import chalk from "chalk"; - - -// Function to count words and display the result -async function countWords(sentence: string) { - const words = sentence.trim().split(" "); - console.log(words); - console.log(chalk.blue(`Your sentence word count is ${words.length}`)); -} - -// Welcome message for the user -console.log(chalk.green("Welcome! Let's count the words in your sentence.")); - -// Prompt the user to enter a sentence -inquirer.prompt([ - { - name: "Sentence", - type: "input", - message: "Enter your sentence to count the words:" - } -]).then(async (answers: { Sentence: string }) => { - await countWords(answers.Sentence); - // Prompt for adding or removing words - while (true) { - const action = await inquirer.prompt([ - { - name: "action", - type: "list", - message: "Do you want to add or remove a word?", - choices: ["Add", "Remove", "Exit"] - } - ]); - if (action.action === "Add") { - const newWord = await inquirer.prompt([ - { - name: "word", - type: "input", - message: "Enter the word to add:" - } - ]); - answers.Sentence += " " + newWord.word; - await countWords(answers.Sentence); - } else if (action.action === "Remove") { - const wordToRemove = await inquirer.prompt([ - { - name: "word", - type: "input", - message: "Enter the word to remove:" - } - ]); - if (answers.Sentence.includes(wordToRemove.word)) { - answers.Sentence = answers.Sentence.replace(new RegExp(`\\b${wordToRemove.word}\\b`, "g"), "").replace(/\s{2,}/g, " ").trim(); - await countWords(answers.Sentence); - } else { - console.log(chalk.red("Sorry, the word you entered is not found in the sentence. Please enter a valid word to remove.")); - } - } else { - console.log(chalk.yellow("Thank you for using our word counter. See you soon!")); - process.exit(0); // Exit the process - } - } -});