Giter Club home page Giter Club logo

javascript-millionaire's Issues

Refactored the code

#!/usr/bin/env node

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

let playerName;
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
async function welcome() {
	const rainbowTitle = chalkAnimation.rainbow(
		"Who wants to be javascript millionaire?"
	);
	await sleep();
	rainbowTitle.stop();
	console.log(`
    ${chalk.bgBlue("HOW TO PLAY")} 
    I am a process on your computer.
    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();
	await sleep();

	if (isCorrect) {
		spinner.success({ text: `Nice work ${playerName}. That's a legit answer` });
	} else {
		spinner.error({ text: `๐Ÿ’€๐Ÿ’€๐Ÿ’€ Game over, you lose ${playerName}!` });
		process.exit(1); //1 is for error,0 is for success
	}
}

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

	playerName = answers.player_name;
}

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(
			chalk.green(
				`Programming isn't about what you know; it's about making the command line look cool`
			)
		);
		process.exit(0);
	});
}

async function question(index) {
	console.log("\n");
	const answers = await inquirer.prompt({
		name: "question",
		type: "list",
		...mcqs[index],
	});

	return handleAnswer(
		answers.question === mcqs[index].choices[mcqs[index].answer]
	);
}
let mcqs = [
	{
		message: 'What is x? var x = 1_1 + "1" + Number(1)\n',
		choices: ["4", '"4"', '"1111"', "69420"],
		answer: 2,
	},
	{
		message: "JavaScript was created in 10 days then released on\n",
		choices: [
			"May 23rd, 1995",
			"Nov 24th, 1995",
			"Dec 4th, 1995",
			"Dec 17, 1996",
		],
		answer: 2,
	},
	{
		message: `What is the first element in the array?\n\t['๐Ÿ', '๐Ÿฆ™', '๐Ÿ'].length = 0\n`,
		choices: ["0", "๐Ÿ", "๐Ÿ", "undefined"],
		answer: 3,
	},
	{
		message: "Which of the following is NOT a primitive type?\n",
		choices: [
			"boolean",
			"number",
			"null",
			"object", // Correct
		],
		answer: 3,
	},
	{
		message: `JS is a high-level single-threaded, garbage-collected,
		interpreted(or just-in-time compiled), prototype-based,
		multi-paradigm, dynamic language with a ____ event loop`,
		choices: ["multi-threaded", "non-blocking", "synchronous", "promise-based"],
		answer: 1,
	},
];

async function main() {
	console.clear();
	await welcome();
	await askName();

	for (let i = 0; i < mcqs.length; i++) {
		await question(i);
	}
	winner();
}
// Run it with top-level await
main();

getting this error

file:///home/heartbeat/.npm/_npx/aab6981678745c7e/node_modules/firequiz/index.js:151
await welcome();
^^^^^

SyntaxError: Unexpected reserved word
at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18)
at async link (internal/modules/esm/module_job.js:42:21)

Not able to run

await welcome();
^^^^^

SyntaxError: Unexpected reserved word

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.