-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuyLoop.js
More file actions
137 lines (110 loc) · 3.69 KB
/
buyLoop.js
File metadata and controls
137 lines (110 loc) · 3.69 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var prompt = require('prompt-sync')();
var playerInfo = [];
var amtToSpend = 1000;
var boughtItems = [];
var inventory = [];
function item(name, cost)
{
this.name = name;
this.cost = cost;
}
var itemsToBuy = [new item("Sword", 200), new item("Shield", 50),
new item("Boots", 60), new item("Health Potion", 60),
new item("Armor", 300), new item("Horse", 400),
new item("Water", 10),
new item("Food", 10)];
function viewStore()
{
console.log("----The STORE----");
console.log();
console.log("{");
for(i = 0; i < itemsToBuy.length; i++){
console.log("***NAME OF ITEM: " + itemsToBuy[i].name +
" | COST OF ITEM: " + itemsToBuy[i].cost + " ***");
}
console.log(" }");
}
function viewInventory(){
console.log("You have these items in your INVENTORY");
console.log();
console.log("{ ***");
for(i = 0; i < inventory.length; i++)
{
console.log(inventory[i].name + " *** ");
console.log();
}
console.log(" }");
console.log();
}
function buy()
{
var confirmBuy = false;
var itemIndex = -1;
var idx = 0;
var ask = prompt("what item would you like to purchase? ");
var found = false;
while(found == false)
{
found = ask == itemsToBuy[idx].name.trim().toLowerCase() && amtToSpend >= itemsToBuy[idx].cost;
if(found == true)
{
confirmBuy == true;
itemIndex = idx;
break;
}
else if( ask == itemsToBuy[idx].name.trim().toLowerCase() && amtToSpend < itemsToBuy[idx].cost){
console.log("You have an insufficient balance: " + amtToSpend+ ", for this item. Purchase something cheaper");
console.log();
console.log("Remeber, the more you buy, the greater your quest! Buy up!");
break;
}
else{
idx++;
}
}
if(found == true)
{
amtToSpend-=itemsToBuy[itemIndex].cost;
inventory.push(itemsToBuy[itemIndex]);
viewInventory();
console.log();
console.log();
console.log("You have " + amtToSpend + " left to spend! Buy quickly, your quest awaits");
console.log();
console.log("AMOUNT LEFT: " + amtToSpend);
}
else{
console.log("That item is not listed or your balance is insufficient");
}
}
playerInfo[0] = prompt("What will your adventure name be? ");
playerInfo[1] = prompt("What shall be your adventerous eptiah. Eg: 'Alfred the Great' ");
console.log("-------Welcome " + playerInfo[1]+ " " + playerInfo[0] + " to the adventure game!-------");
console.log();
console.log("***You will start out with 1000$, and will buy itmes at the shop before going on this adventure!***");
console.log();
console.log("***You must spend as much money as you can! NOTHING SHOULD BE LEFT!***");
console.log();
console.log();
var startGame = prompt("Are you ready to begin thy quest? YES/NO? ");
console.log();
if(startGame.toLowerCase() == "yes")
{
console.log("***You will start out with 1000$, and will buy itmes at the shop before going on this adventure!***");
console.log();
console.log("***You must spend as much money as you can! NOTHING SHOULD BE LEFT!***");
while(amtToSpend > 0)
{
viewStore();
buy();
if(amtToSpend == 0){
break;
}
}
console.log();
console.log("You have reached your limit! Venture upon your journey, " + playerInfo[0]);
}
else{
console.log();
console.log("Until next time! " + playerInfo[0]);
}