|
1 |
| -import * as readline from 'readline' |
| 1 | +#!/usr/bin/env node |
2 | 2 |
|
3 |
| -// start reusable code |
4 |
| -async function input(prompt = "") { |
5 |
| - const rl = readline.createInterface({ |
6 |
| - input: process.stdin, |
7 |
| - output: process.stdout |
8 |
| - }) |
| 3 | +import { println, input } from '../../00_Common/javascript/common.mjs'; |
9 | 4 |
|
10 |
| - return new Promise((resolve, _) => { |
11 |
| - rl.setPrompt(prompt) |
12 |
| - // show user the question |
13 |
| - rl.prompt() |
14 |
| - // listen for user answer, |
15 |
| - // callback is triggered as soon as user hits enter key |
16 |
| - rl.on('line', answer => { |
17 |
| - rl.close() |
18 |
| - // resolve the promise, with the input the user entered |
19 |
| - resolve(answer) |
20 |
| - }) |
21 |
| - }) |
22 |
| -} |
23 |
| - |
24 |
| -function println(message = "", align = "left"){ |
25 |
| - let padColCount = 0 |
26 |
| - if(align === "center"){ |
| 5 | +function printAlign(message = "", align = "left") { |
| 6 | + // process.stdout.columns is the number of spaces per line in the terminal |
| 7 | + const maxWidth = process.stdout.columns |
| 8 | + if (align === "center") { |
27 | 9 | // calculate the amount of spaces required to center the message
|
28 |
| - // process.stdout.columns is the number of spaces per line in the terminal |
29 |
| - padColCount = Math.round(process.stdout.columns / 2 + message.length / 2) |
| 10 | + const padColCount = Math.round((process.stdout.columns-message.length)/2); |
| 11 | + const padding = padColCount <= 0 ? '' : ' '.repeat(padColCount); |
| 12 | + println(padding, message); |
| 13 | + } else if (align === "right") { |
| 14 | + const padColCount = Math.round(process.stdout.columns-message.length); |
| 15 | + const padding = padColCount <= 0 ? '' : ' '.repeat(padColCount); |
| 16 | + println(padding, message); |
| 17 | + } else { |
| 18 | + println(message); |
30 | 19 | }
|
31 |
| - console.log(message.padStart(padColCount, " ")) |
32 | 20 | }
|
33 |
| -// end reusable code |
34 |
| - |
35 | 21 |
|
36 | 22 | function equalIgnoreCase(correct, provided){
|
37 | 23 | return correct.toString().toLowerCase() === provided.toString().toLowerCase()
|
38 | 24 | }
|
39 | 25 |
|
40 | 26 | async function evaluateQuestion(question, answerOptions, correctAnswer, correctMessage, wrongMessage){
|
41 | 27 | // ask the user to answer the given question
|
| 28 | + println(question); |
| 29 | + println(answerOptions.map((answer, index) => `${index+1})${answer}`).join(', ')); |
42 | 30 | // this is a blocking wait
|
43 |
| - const answer = await input(question + "\n" + answerOptions + "\n") |
| 31 | + const answer = await input('?') |
44 | 32 | const isCorrect = equalIgnoreCase(correctAnswer, answer)
|
45 | 33 | println(isCorrect ? correctMessage : wrongMessage)
|
46 | 34 | return isCorrect ? 1 : 0
|
47 | 35 | }
|
48 | 36 |
|
49 | 37 | async function main(){
|
50 | 38 | let score = 0
|
51 |
| - println("LITERATURE QUIZ", "center") |
52 |
| - println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY", "center") |
53 |
| - println();println();println() |
| 39 | + |
| 40 | + printAlign("LITERATURE QUIZ", "center") |
| 41 | + printAlign("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY", "center") |
| 42 | + println("\n\n") |
| 43 | + |
| 44 | + println("TEST YOUR KNOWLEDGE OF CHILDREN'S LITERATURE."); |
| 45 | + println(); |
| 46 | + println("THIS IS A MULTIPLE-CHOICE QUIZ."); |
| 47 | + println("TYPE A 1, 2, 3, OR 4 AFTER THE QUESTION MARK."); |
| 48 | + println(); |
| 49 | + println("GOOD LUCK!"); |
| 50 | + println("\n\n"); |
54 | 51 |
|
55 | 52 | score += await evaluateQuestion("IN PINOCCHIO, WHAT WAS THE NAME OF THE CAT?",
|
56 |
| - "1)TIGGER, 2)CICERO, 3)FIGARO, 4)GUIPETTO", 3, |
| 53 | + [ "TIGGER", "CICERO", "FIGARO", "GUIPETTO"], 3, |
57 | 54 | "VERY GOOD! HERE'S ANOTHER.", "SORRY...FIGARO WAS HIS NAME.")
|
58 | 55 | println()
|
59 | 56 |
|
60 | 57 | score += await evaluateQuestion("FROM WHOSE GARDEN DID BUGS BUNNY STEAL THE CARROTS?",
|
61 |
| - "1)MR. NIXON'S, 2)ELMER FUDD'S, 3)CLEM JUDD'S, 4)STROMBOLI'S", 2, |
| 58 | + [ "MR. NIXON'S", "ELMER FUDD'S", "CLEM JUDD'S", "STROMBOLI'S" ], 2, |
62 | 59 | "PRETTY GOOD!", "TOO BAD...IT WAS ELMER FUDD'S GARDEN.")
|
63 | 60 | println()
|
64 | 61 |
|
65 | 62 | score += await evaluateQuestion("IN THE WIZARD OF OS, DOROTHY'S DOG WAS NAMED",
|
66 |
| - "1)CICERO, 2)TRIXIA, 3)KING, 4)TOTO", 4, |
| 63 | + [ "CICERO", "TRIXIA", "KING", "TOTO" ], 4, |
67 | 64 | "YEA! YOU'RE A REAL LITERATURE GIANT.",
|
68 | 65 | "BACK TO THE BOOKS,...TOTO WAS HIS NAME.")
|
69 | 66 | println()
|
70 | 67 |
|
71 | 68 | score += await evaluateQuestion("WHO WAS THE FAIR MAIDEN WHO ATE THE POISON APPLE",
|
72 |
| - "1)SLEEPING BEAUTY, 2)CINDERELLA, 3)SNOW WHITE, 4)WENDY", 3, |
| 69 | + [ "SLEEPING BEAUTY", "CINDERELLA", "SNOW WHITE", "WENDY" ], 3, |
73 | 70 | "GOOD MEMORY!", "OH, COME ON NOW...IT WAS SNOW WHITE.")
|
74 | 71 |
|
75 |
| - println();println() |
| 72 | + println("\n") |
76 | 73 |
|
77 | 74 | if(score === 4) {
|
78 | 75 | println("WOW! THAT'S SUPER! YOU REALLY KNOW YOUR NURSERY\n"+
|
|
0 commit comments