Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 73 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node


// /*
// /$$$$$$$$ /$$$$$$ / $$$$$$$ / $$$$$$$$ / $$$$$$ / $$ / $$ / $$$$$$ / $$$$$$$ / $$
// | $$_____/|_ $$_/| $$__ $$| $$_____/ /$$__ $$| $$ | $$|_ $$_/| $$__ $$|__/
Expand All @@ -12,54 +11,65 @@
// |__/ |______/|__/ |__/|________/ \______/ |__/ |__/|______/|__/|__/ |__/ \______/
// */


import chalk from 'chalk';
import inquirer from 'inquirer';
import gradient from 'gradient-string';
import chalkAnimation from 'chalk-animation';
import figlet from 'figlet';
import { createSpinner } from 'nanospinner';
import chalk from "chalk";
import inquirer from "inquirer";
import gradient from "gradient-string";
import chalkAnimation from "chalk-animation";
import figlet from "figlet";
import { createSpinner } from "nanospinner";

let playerName;
let playerScore = 0;
let totalScore = 5;

const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));

async function welcome() {
const rainbowTitle = chalkAnimation.rainbow(
'Who Wants To Be A JavaScript Millionaire? \n'
"Who Wants To Be A JavaScript Millionaire? \n"
);

await sleep();
rainbowTitle.stop();

console.log(`
${chalk.bgBlue('HOW TO PLAY')}
${chalk.bgBlue("HOW TO PLAY")}
I am a process on your computer.
If you get any question wrong I will be ${chalk.bgRed('killed')}
If you get any question wrong I will be ${chalk.bgRed("killed")}
So get all the questions right...

`);
}

async function handleAnswer(isCorrect) {
const spinner = createSpinner('Checking answer...').start();
async function handleAnswer(isCorrect, successMsg) {
const spinner = createSpinner("Checking answer...").start();
await sleep();

if (isCorrect) {
spinner.success({ text: `Nice work ${playerName}. That's a legit answer` });
playerScore += 1;
spinner.success({
text: successMsg,
});
console.log(chalk.yellow(` Score : ${playerScore} / ${totalScore} 🎉\n`));
} else {
spinner.error({ text: `💀💀💀 Game over, you lose ${playerName}!` });
console.log(
chalk.bgRed(
chalk.yellow(`You scored ${playerScore} out of ${totalScore}`)
)
);
process.exit(1);
}
}

async function askName() {
const answers = await inquirer.prompt({
name: 'player_name',
type: 'input',
message: 'What is your name?',
name: "player_name",
mark: "aa",
type: "input",
message: "What is your name?",
default() {
return 'Player';
return "Player";
},
});

Expand All @@ -69,7 +79,7 @@ async function askName() {
function winner() {
console.clear();
figlet(`Congrats , ${playerName} !\n $ 1 , 0 0 0 , 0 0 0`, (err, data) => {
console.log(gradient.pastel.multiline(data) + '\n');
console.log(gradient.pastel.multiline(data) + "\n");

console.log(
chalk.green(
Expand All @@ -81,69 +91,81 @@ function winner() {
}

async function question1() {
console.log(chalk.green(`\n Here we go. Question ${playerScore + 1} ->`));
const answers = await inquirer.prompt({
name: 'question_1',
type: 'list',
message: 'JavaScript was created in 10 days then released on\n',
name: "question_1",
type: "list",
message: "JavaScript was created in 10 days then released on\n",
choices: [
'May 23rd, 1995',
'Nov 24th, 1995',
'Dec 4th, 1995',
'Dec 17, 1996',
"May 23rd, 1995",
"Nov 24th, 1995",
"Dec 4th, 1995",
"Dec 17, 1996",
],
});
const successMsg = `Nice work ${playerName}. That's a legit answer.`;

return handleAnswer(answers.question_1 === 'Dec 4th, 1995');
return handleAnswer(answers.question_1 === "Dec 4th, 1995", successMsg);
}

async function question2() {
console.log(chalk.green(`\n Question ${playerScore + 1} ->`));
const answers = await inquirer.prompt({
name: 'question_2',
type: 'list',
name: "question_2",
type: "list",
message: 'What is x? var x = 1_1 + "1" + Number(1)\n',
choices: ['4', '"4"', '"1111"', '69420'],
choices: ["4", '"4"', '"1111"', "69420"],
});
return handleAnswer(answers.question_2 === '"1111"');
const successMsg = `Good going ${playerName}! Let's hop on to the next one..`;

return handleAnswer(answers.question_2 === '"1111"', successMsg);
}

async function question3() {
console.log(chalk.green(`\n Question ${playerScore + 1} ->`));
const answers = await inquirer.prompt({
name: 'question_3',
type: 'list',
name: "question_3",
type: "list",
message: `What is the first element in the array? ['🐏', '🦙', '🐍'].length = 0\n`,
choices: ['0', '🐏', '🐍', 'undefined'],
choices: ["0", "🐏", "🐍", "undefined"],
});
const successMsg = `Someone call the fire department coz ${playerName} is on firee!!`;

return handleAnswer(answers.question_3 === 'undefined');
return handleAnswer(answers.question_3 === "undefined", successMsg);
}

async function question4() {
console.log(chalk.green(`\n Question ${playerScore + 1} ->`));
const answers = await inquirer.prompt({
name: 'question_4',
type: 'list',
message: 'Which of the following is NOT a primitive type?\n',
name: "question_4",
type: "list",
message: "Which of the following is NOT a primitive type?\n",
choices: [
'boolean',
'number',
'null',
'object', // Correct
"boolean",
"number",
"null",
"object", // Correct
],
});
return handleAnswer(answers.question_4 === 'object');
const successMsg = `You're close ${playerName}. Just one left. 💪`;

return handleAnswer(answers.question_4 === "object", successMsg);
}

async function question5() {
console.log(chalk.green(`\n Question ${playerScore + 1} ->`));
const answers = await inquirer.prompt({
name: 'question_5',
type: 'list',
name: "question_5",
type: "list",
message:
'JS is a high-level single-threaded, garbage-collected,\n' +
'interpreted(or just-in-time compiled), prototype-based,\n' +
'multi-paradigm, dynamic language with a ____ event loop\n',
choices: ['multi-threaded', 'non-blocking', 'synchronous', 'promise-based'],
"JS is a high-level single-threaded, garbage-collected,\n" +
"interpreted(or just-in-time compiled), prototype-based,\n" +
"multi-paradigm, dynamic language with a ____ event loop\n",
choices: ["multi-threaded", "non-blocking", "synchronous", "promise-based"],
});
const successMsg = `${playerName} becomes the Javascript Millionaire!!`;

return handleAnswer(answers.question_5 === 'non-blocking');
return handleAnswer(answers.question_5 === "non-blocking", successMsg);
}

// Run it with top-level await
Expand All @@ -155,4 +177,4 @@ await question2();
await question3();
await question4();
await question5();
winner();
winner();
Loading