forked from miketully426/wpluslivecode2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass5.js
More file actions
55 lines (44 loc) · 1.47 KB
/
class5.js
File metadata and controls
55 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const input = require('readline-sync');
let keepGoing = true;
function runProgram() {
while (keepGoing) {
if (askIfWantToBuy() !== 'y') {
console.log("I don't want to sell to you anyway!");
keepGoing = false;
break;
}
console.log('How many books do you want to buy?');
const numberToBuy = input.question('Enter the number of books: ')
const booksToBuy = [
'Charlie the Cholocate Factory',
'A Court of Thorns and Roses',
'Starling House',
'House of Loaves'
]
const booksBought = [];
for (let index = 0; index < numberToBuy; index++) {
showBookList(booksToBuy);
booksBought.push(selectBook(booksToBuy));
}
console.log(`You bought: ${booksBought.join(',')}`);
const leave = input.question('Do you want to start over? (y/n)');
if (leave === 'n') {
keepGoing = false;
}
}
}
function askIfWantToBuy() {
console.log('Welcome to Amazon: The Original');
const wantToBuy = input.question('Do you want to buy books? (y/n)');
return wantToBuy;
}
function showBookList(bookList) {
for (let indexj = 0; indexj < bookList.length; indexj++) {
console.log(`${indexj + 1}: ${bookList[indexj]}`);
}
}
function selectBook(bookList) {
const bookChoice = input.question('What book do you want?');
return bookList[bookChoice - 1]
}
runProgram();