-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge 6 (2).js
136 lines (101 loc) · 2.98 KB
/
Challenge 6 (2).js
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
135
136
let groceryList = {};
let groceryInput, submit, quantityInput, removeButton;
let itemIndex = [];
let foundItem = true;
let tooManyItem = false;
let duplicateItem = false;
let listYValue = 0;
function setup() {
createCanvas(900, 500);
groceryInput = createInput();
groceryInput.position(110, 55);
quantityInput = createInput();
quantityInput.position(110, 95);
submit = createButton("Add");
submit.position(300, 55);
submit.mousePressed(pressAdd);
removeButton = createButton("Remove");
removeButton.position(350, 55);
removeButton.mousePressed(pressRemove);
}
function draw() {
background(255);
fill(0);
textSize(20);
text("Item: ", 20, 65);
text("Current List: ", 20, 150);
text("Quantity:", 20, 105)
listYValue = 0
// Displays all the items in the list
for (let i in groceryList) {
// changing text colour if the item is apples or chicken
if (i == "apples" || i == "chicken") {
fill(0, 255, 0);
}
else {
fill(0);
}
// actually displaying the text
text(i + ": " + groceryList[i], 150, 150 + listYValue * 20);
// add y value
listYValue += 1
}
// checking if we haven't found the item
if (!foundItem) {
text("Item is not in your list", 100, 25);
}
if (duplicateItem) {
text("Item is already in your list!", 100, 25);
}
if (tooManyItem) {
text("Too many items!", 100, 25);
}
}
function pressAdd() {
// reset duplicateItem and tooManyItem so text doesn't overlap
duplicateItem = false;
tooManyItem = false;
// check if the length of the list is under 10 items and doesn't have the item already
if (Object.keys(groceryList).length < 10) {
// checking if its already in the list
if (!(groceryInput.value() in groceryList)) {
// add the item to the list
groceryList[groceryInput.value()] = quantityInput.value();
}
// add the quantity onto it
else {
groceryList[groceryInput.value()] = Number(groceryList[groceryInput.value()]) + Number(quantityInput.value())
}
}
// display tooManyItem text
else {
tooManyItem = true;
}
// reset the foundItem thing if they added something
foundItem = true;
}
function pressRemove() {
// reset these variables so text doesn't overlap
duplicateItem = false;
tooManyItem = false;
// check if the item is even in the list
if (groceryInput.value() in groceryList) {
// check if the item has more quantity than the quantity we are removing
if (Number(groceryList[groceryInput.value()]) > Number(quantityInput.value())){
// minus it from the groceryList
groceryList[groceryInput.value()] -= Number(quantityInput.value())
}
// they don't have enough so we delete it
else{
// remove it from the Object
delete groceryList[groceryInput.value()]
}
// reset foundItem
foundItem = true;
}
// display a message if it doesn't exist
else {
// set variable to false to signify that we haven't found the item
foundItem = false;
}
}