Skip to content

Instantly share code, notes, and snippets.

@alex289
Last active February 7, 2024 14:36
Show Gist options
  • Save alex289/831fb869367e163b6c304b00f9013729 to your computer and use it in GitHub Desktop.
Save alex289/831fb869367e163b6c304b00f9013729 to your computer and use it in GitHub Desktop.
Little cli game to learn the greek alphabet
const readline = require("readline");
const greekAlphabet = {
Α: "Alpha",
Β: "Beta",
Γ: "Gamma",
Δ: "Delta",
Ε: "Epsilon",
Ζ: "Zeta",
Η: "Eta",
Θ: "Theta",
Ι: "Iota",
Κ: "Kappa",
Λ: "Lambda",
Μ: "Mu",
Ν: "Nu",
Ξ: "Xi",
Ο: "Omicron",
Π: "Pi",
Ρ: "Rho",
Σ: "Sigma",
Τ: "Tau",
Υ: "Upsilon",
Φ: "Phi",
Χ: "Chi",
Ψ: "Psi",
Ω: "Omega",
α: "Alpha",
β: "Beta",
γ: "Gamma",
δ: "Delta",
ε: "Epsilon",
ζ: "Zeta",
η: "Eta",
θ: "Theta",
ι: "Iota",
κ: "Kappa",
λ: "Lambda",
μ: "Mu",
ν: "Nu",
ξ: "Xi",
ο: "Omicron",
π: "Pi",
ρ: "Rho",
σ: "Sigma",
τ: "Tau",
υ: "Upsilon",
φ: "Phi",
χ: "Chi",
ψ: "Psi",
ω: "Omega",
};
const letterAmount = Object.keys(greekAlphabet).length;
let mistakes = 0;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function getRandomLetter() {
const keys = Object.keys(greekAlphabet);
if (keys.length === 0) {
return null;
}
const randomIndex = Math.floor(Math.random() * keys.length);
return keys[randomIndex];
}
function askQuestion() {
const randomLetter = getRandomLetter();
if (randomLetter) {
const correctAnswer = greekAlphabet[randomLetter];
rl.question(
`${randomLetter} (${
Object.keys(greekAlphabet).length
}/${letterAmount}): `,
(answer) => {
if (answer.toLowerCase() === correctAnswer.toLowerCase()) {
console.log("Correct! Well done.\n");
// Mark the question as asked
delete greekAlphabet[randomLetter];
} else {
console.log(`Incorrect. The correct answer is ${correctAnswer}.\n`);
mistakes++;
}
askQuestion();
},
);
} else {
console.log("Congratulations! You've answered all the questions.\n");
console.log(`You made ${mistakes} mistake(s).`);
rl.close();
}
}
console.log("Let's quiz on the Greek alphabet!\n");
askQuestion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment