@bruxx/cli-tool
Version:
`BRX TEMPLATE` is a production-ready boilerplate for modern React projects. It eliminates the tedious setup process, letting you jump straight into coding with a preconfigured, optimized environment. Built with best practices, it’s designed for scalabilit
165 lines (142 loc) • 4.66 kB
JavaScript
import * as p from "@clack/prompts";
import fs from "fs";
import { setTimeout } from "node:timers/promises";
import path from "path";
import pc from "picocolors";
let totalCorrect = 0;
const userAnswers = [];
let userName = "";
process.on("SIGINT", () => {
p.cancel("CLI stopped by user (Ctrl + C). Exiting...");
process.exit(0);
});
async function askUserName() {
userName = await p.text({
message: "What is your name?",
validate: (value) => (value ? undefined : "Name cannot be empty."),
});
}
async function askQuestion(question, answers, correctAnswerIndex) {
const options = answers.map((answer) => ({ value: answer, label: answer }));
const s = p.spinner();
s.start("Loading... ♻️");
await setTimeout(1000);
s.stop("Done ✅!");
const answer = await p.select({
message: question,
initialValue: "1",
options: options,
});
userAnswers.push({
question,
selectedAnswer: answer,
correctAnswer: answers[correctAnswerIndex],
});
if (answer === answers[correctAnswerIndex]) {
totalCorrect++;
}
}
class Question {
constructor(question, answerArray) {
this.question = question;
this.answerArray = [...answerArray];
this.correctAnswerIndex = Math.floor(Math.random() * answerArray.length);
// Ensure the correct answer is placed at the randomly selected index
[this.answerArray[0], this.answerArray[this.correctAnswerIndex]] = [
this.answerArray[this.correctAnswerIndex],
this.answerArray[0],
];
}
}
function saveResultsToFile(questions, userAnswers, totalCorrect, userName) {
const results = {
name: userName,
totalCorrect,
totalQuestions: questions.length,
answers: userAnswers,
};
const dir = path.join(process.cwd(), "answers");
const filePath = path.join(dir, `${userName}_results.json`);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFileSync(filePath, JSON.stringify(results, null, 2));
}
async function main() {
p.intro(`${pc.bgMagenta(pc.black("Welcome to the CLI Tech Quiz!"))}`);
await askUserName();
const questions = [
new Question("What does AI stand for?", [
"Artificial Intelligence",
"Automated Integration",
"Advanced Interface",
"Algorithmic Innovation",
]),
new Question(
"Which programming language is commonly used for AI development?",
["Python", "Java", "C++", "Ruby"]
),
new Question("Who is known as the father of AI?", [
"Alan Turing",
"Elon Musk",
"Bill Gates",
"Jeff Bezos",
]),
new Question("What is the main goal of machine learning?", [
"To enable machines to learn from data",
"To replace human intelligence",
"To build faster computers",
"To create robots",
]),
new Question("Which company developed the AI model GPT?", [
"OpenAI",
"Google",
"Microsoft",
"Facebook",
]),
new Question("What is a neural network?", [
"A computing system inspired by biological neural networks",
"A physical network of connected neurons",
"A type of database",
"A social media trend",
]),
new Question(
"Which of these is an example of an AI-powered voice assistant?",
["Siri", "Firefox", "Excel", "Photoshop"]
),
new Question("What is the Turing Test used for?", [
"To determine if a machine can exhibit intelligent behavior",
"To measure CPU speed",
"To test software usability",
"To evaluate network security",
]),
new Question(
"Which field combines AI with large amounts of data to extract insights?",
["Data Science", "Mechanical Engineering", "Astrophysics", "Psychology"]
),
new Question("What is deep learning?", [
"A subset of machine learning that uses neural networks",
"A method for improving memory retention",
"A technique for software debugging",
"A way to store large datasets",
]),
];
for (const question of questions) {
await askQuestion(
question.question,
question.answerArray,
question.correctAnswerIndex
);
}
p.outro(
pc.green(`You got ${totalCorrect} out of ${questions.length} correct!`)
);
if (totalCorrect === questions.length) {
p.outro("Congratulations! You got all the questions correct!");
} else {
p.outro("Better luck next time!");
}
saveResultsToFile(questions, userAnswers, totalCorrect, userName);
}
main();