mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-05 17:48:52 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
3
57_Literature_Quiz/javascript/README.md
Normal file
3
57_Literature_Quiz/javascript/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells)
|
||||
90
57_Literature_Quiz/javascript/literature-quiz-node.mjs
Normal file
90
57_Literature_Quiz/javascript/literature-quiz-node.mjs
Normal file
@@ -0,0 +1,90 @@
|
||||
import * as readline from 'readline'
|
||||
|
||||
// start reusable code
|
||||
async function input(prompt = "") {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
})
|
||||
|
||||
return new Promise((resolve, _) => {
|
||||
rl.setPrompt(prompt)
|
||||
// show user the question
|
||||
rl.prompt()
|
||||
// listen for user answer,
|
||||
// callback is triggered as soon as user hits enter key
|
||||
rl.on('line', answer => {
|
||||
rl.close()
|
||||
// resolve the promise, with the input the user entered
|
||||
resolve(answer)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function println(message = "", align = "left"){
|
||||
let padColCount = 0
|
||||
if(align === "center"){
|
||||
// calculate the amount of spaces required to center the message
|
||||
// process.stdout.columns is the number of spaces per line in the terminal
|
||||
padColCount = Math.round(process.stdout.columns / 2 + message.length / 2)
|
||||
}
|
||||
console.log(message.padStart(padColCount, " "))
|
||||
}
|
||||
// end reusable code
|
||||
|
||||
|
||||
function equalIgnoreCase(correct, provided){
|
||||
return correct.toString().toLowerCase() === provided.toString().toLowerCase()
|
||||
}
|
||||
|
||||
async function evaluateQuestion(question, answerOptions, correctAnswer, correctMessage, wrongMessage){
|
||||
// ask the user to answer the given question
|
||||
// this is a blocking wait
|
||||
const answer = await input(question + "\n" + answerOptions + "\n")
|
||||
const isCorrect = equalIgnoreCase(correctAnswer, answer)
|
||||
println(isCorrect ? correctMessage : wrongMessage)
|
||||
return isCorrect ? 1 : 0
|
||||
}
|
||||
|
||||
async function main(){
|
||||
let score = 0
|
||||
println("LITERATURE QUIZ", "center")
|
||||
println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY", "center")
|
||||
println();println();println()
|
||||
|
||||
score += await evaluateQuestion("IN PINOCCHIO, WHAT WAS THE NAME OF THE CAT?",
|
||||
"1)TIGGER, 2)CICERO, 3)FIGARO, 4)GUIPETTO", 3,
|
||||
"VERY GOOD! HERE'S ANOTHER.", "SORRY...FIGARO WAS HIS NAME.")
|
||||
println()
|
||||
|
||||
score += await evaluateQuestion("FROM WHOSE GARDEN DID BUGS BUNNY STEAL THE CARROTS?",
|
||||
"1)MR. NIXON'S, 2)ELMER FUDD'S, 3)CLEM JUDD'S, 4)STROMBOLI'S", 2,
|
||||
"PRETTY GOOD!", "TOO BAD...IT WAS ELMER FUDD'S GARDEN.")
|
||||
println()
|
||||
|
||||
score += await evaluateQuestion("IN THE WIZARD OF OS, DOROTHY'S DOG WAS NAMED",
|
||||
"1)CICERO, 2)TRIXIA, 3)KING, 4)TOTO", 4,
|
||||
"YEA! YOU'RE A REAL LITERATURE GIANT.",
|
||||
"BACK TO THE BOOKS,...TOTO WAS HIS NAME.")
|
||||
println()
|
||||
|
||||
score += await evaluateQuestion("WHO WAS THE FAIR MAIDEN WHO ATE THE POISON APPLE",
|
||||
"1)SLEEPING BEAUTY, 2)CINDERELLA, 3)SNOW WHITE, 4)WENDY", 3,
|
||||
"GOOD MEMORY!", "OH, COME ON NOW...IT WAS SNOW WHITE.")
|
||||
|
||||
println();println()
|
||||
|
||||
if(score === 4) {
|
||||
println("WOW! THAT'S SUPER! YOU REALLY KNOW YOUR NURSERY\n"+
|
||||
"YOUR NEXT QUIZ WILL BE ON 2ND CENTURY CHINESE\n"+
|
||||
"LITERATURE (HA, HA, HA)")
|
||||
} else if(score <= 2){
|
||||
println("UGH. THAT WAS DEFINITELY NOT TOO SWIFT. BACK TO\n" +
|
||||
"NURSERY SCHOOL FOR YOU, MY FRIEND.")
|
||||
} else {
|
||||
println("NOT BAD, BUT YOU MIGHT SPEND A LITTLE MORE TIME\n"+
|
||||
"READING THE NURSERY GREATS.")
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
9
57_Literature_Quiz/javascript/litquiz.html
Normal file
9
57_Literature_Quiz/javascript/litquiz.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>LITERATURE QUIZ</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="litquiz.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
119
57_Literature_Quiz/javascript/litquiz.js
Normal file
119
57_Literature_Quiz/javascript/litquiz.js
Normal file
@@ -0,0 +1,119 @@
|
||||
// LITQUIZ
|
||||
//
|
||||
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
||||
//
|
||||
|
||||
function print(str)
|
||||
{
|
||||
document.getElementById("output").appendChild(document.createTextNode(str));
|
||||
}
|
||||
|
||||
function input()
|
||||
{
|
||||
var input_element;
|
||||
var input_str;
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
input_element = document.createElement("INPUT");
|
||||
|
||||
print("? ");
|
||||
input_element.setAttribute("type", "text");
|
||||
input_element.setAttribute("length", "50");
|
||||
document.getElementById("output").appendChild(input_element);
|
||||
input_element.focus();
|
||||
input_str = undefined;
|
||||
input_element.addEventListener("keydown", function (event) {
|
||||
if (event.keyCode == 13) {
|
||||
input_str = input_element.value;
|
||||
document.getElementById("output").removeChild(input_element);
|
||||
print(input_str);
|
||||
print("\n");
|
||||
resolve(input_str);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tab(space)
|
||||
{
|
||||
var str = "";
|
||||
while (space-- > 0)
|
||||
str += " ";
|
||||
return str;
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(25) + "LITERATURE QUIZ\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
r = 0;
|
||||
print("TEST YOUR KNOWLEDGE OF CHILDREN'S LITERATURE.\n");
|
||||
print("\n");
|
||||
print("THIS IS A MULTIPLE-CHOICE QUIZ.\n");
|
||||
print("TYPE A 1, 2, 3, OR 4 AFTER THE QUESTION MARK.\n");
|
||||
print("\n");
|
||||
print("GOOD LUCK!\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("IN PINOCCHIO, WHAT WAS THE NAME OF THE CAT\n");
|
||||
print("1)TIGGER, 2)CICERO, 3)FIGARO, 4)GUIPETTO\n");
|
||||
a = parseInt(await input());
|
||||
if (a == 3) {
|
||||
print("VERY GOOD! HERE'S ANOTHER.\n");
|
||||
r++;
|
||||
} else {
|
||||
print("SORRY...FIGARO WAS HIS NAME.\n");
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("FROM WHOSE GARDEN DID BUGS BUNNY STEAL THE CARROTS?\n");
|
||||
print("1)MR. NIXON'S, 2)ELMER FUDD'S, 3)CLEM JUDD'S, 4)STROMBOLI'S\n");
|
||||
a = parseInt(await input());
|
||||
if (a == 2) {
|
||||
print("PRETTY GOOD!\n");
|
||||
r++;
|
||||
} else {
|
||||
print("TOO BAD...IT WAS ELMER FUDD'S GARDEN.\n");
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("IN THE WIZARD OF OS, DOROTHY'S DOG WAS NAMED\n");
|
||||
print("1)CICERO, 2)TRIXIA, 3)KING, 4)TOTO\n");
|
||||
a = parseInt(await input());
|
||||
if (a == 4) {
|
||||
print("YEA! YOU'RE A REAL LITERATURE GIANT.\n");
|
||||
r++;
|
||||
} else {
|
||||
print("BACK TO THE BOOKS,...TOTO WAS HIS NAME.\n");
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("WHO WAS THE FAIR MAIDEN WHO ATE THE POISON APPLE\n");
|
||||
print("1)SLEEPING BEAUTY, 2)CINDERELLA, 3)SNOW WHITE, 4)WENDY\n");
|
||||
a = parseInt(await input());
|
||||
if (a == 3) {
|
||||
print("GOOD MEMORY!\n");
|
||||
r++;
|
||||
} else {
|
||||
print("OH, COME ON NOW...IT WAS SNOW WHITE.\n");
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
if (r == 4) {
|
||||
print("WOW! THAT'S SUPER! YOU REALLY KNOW YOUR NURSERY\n");
|
||||
print("YOUR NEXT QUIZ WILL BE ON 2ND CENTURY CHINESE\n");
|
||||
print("LITERATURE (HA, HA, HA)\n");
|
||||
} else if (r < 2) {
|
||||
print("UGH. THAT WAS DEFINITELY NOT TOO SWIFT. BACK TO\n");
|
||||
print("NURSERY SCHOOL FOR YOU, MY FRIEND.\n");
|
||||
} else {
|
||||
print("NOT BAD, BUT YOU MIGHT SPEND A LITTLE MORE TIME\n");
|
||||
print("READING THE NURSERY GREATS.\n");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user