MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

javascript_games/ninetyninebottles2.js:34–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

32
33
34async function main() {
35 console.log('niNety-nniinE BoOttels, by Al Sweigart al@inventwithpython.com');
36 console.log();
37 console.log('(Press Ctrl-C to quit.)');
38
39 await sleep(2);
40
41 let bottles = 99; // This is the starting number of bottles.
42
43 // This list holds the string used for the lyrics:
44 let lines = [' bottles of milk on the wall,',
45 ' bottles of milk,',
46 'Take one down, pass it around,',
47 ' bottles of milk on the wall!'];
48
49 while (bottles > 0) { // Keep looping and display the lyrics.
50 await slowPrint(bottles.toString() + lines[0], SPEED)
51 await sleep(LINE_PAUSE);
52 await slowPrint(bottles.toString() + lines[1], SPEED)
53 await sleep(LINE_PAUSE);
54 await slowPrint(lines[2], SPEED)
55 await sleep(LINE_PAUSE);
56 bottles = bottles - 1 ; // Decrease the number of bottles by one.
57
58 if (bottles > 0) { // Print the last line of the current stanza.
59 await slowPrint(bottles.toString() + lines[3], SPEED)
60 } else { // Print the last line of the entire song.
61 await slowPrint('No more bottles of milk on the wall!', SPEED)
62 }
63
64 await sleep(LINE_PAUSE);
65 console.log(); // Print a newline.
66
67 // Choose a random line to make "sillier":
68 let lineNum = Math.floor(Math.random() * 4);
69
70 let line = lines[lineNum];
71 let effect = Math.floor(Math.random() * 4);
72
73 if (effect === 0) { // Replace a character with a space.
74 let charIndex = Math.floor(Math.random() * line.length);
75 line = replaceAt(line, charIndex, ' ');
76 } else if (effect === 1) { // Change the casing of a character.
77 let charIndex = Math.floor(Math.random() * line.length);
78 if (line[charIndex].toUpperCase() == line[charIndex]) {
79 line = replaceAt(line, charIndex, line[charIndex].toLowerCase());
80 } else if (line[charIndex].toLowerCase() == line[charIndex]) {
81 line = replaceAt(line, charIndex, line[charIndex].toUpperCase());
82 }
83 } else if (effect === 2) { // Transpose two characters.
84 let charIndex = Math.floor(Math.random() * (line.length - 1));
85 let firstChar = line[charIndex]
86 let secondChar = line[charIndex + 1]
87 line = replaceAt(line, charIndex, secondChar);
88 line = replaceAt(line, charIndex + 1, firstChar);
89 } else if (effect === 3) { // Double a character.
90 let charIndex = Math.floor(Math.random() * (line.length - 1));
91 line = line.substr(0, charIndex) + line[charIndex] + line.substr(charIndex);

Callers 1

Calls 3

replaceAtFunction · 0.85
sleepFunction · 0.70
slowPrintFunction · 0.70

Tested by

no test coverage detected