-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (120 loc) · 4.71 KB
/
script.js
File metadata and controls
136 lines (120 loc) · 4.71 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
135
var currentDate = Date();
var dateArray = currentDate.split(" ");
var month = dateArray[1];
console.log(month);
var currSeason = getSeason(month);
var outOfSeasonList = [];
// refers to <main>
const app = new Vue ({
el: "#app",
data: {
inputText: "",
groceryList: [],
},
methods: {
// adds item to groceryList
addItem(item) {
item = item.toLowerCase().trim(); // make case insensitive
if (membershipCheck(item) == true) {
this.groceryList.push( { name: item } ) // add to groceryList
this.outOfSeasonList = getOutOfSeason(this.groceryList); // update
}
else {
alert("Unfortunately, this item is not in our database. Make sure your item is plural.");
}
this.inputText = "" // clear input box
},
// removes item from groceryList
deleteItem(item) {
this.groceryList.splice(this.groceryList.findIndex( i => i.name === item.name ), 1)
},
isOutOfSeason(item) {
if (this.outOfSeasonList.findIndex( i => i.name === item.name ) != -1) {
return "red";
}
else {
return "none";
}
},
clearLists() {
this.groceryList = [];
this.outOfSeasonList = [];
}
}
})
function getSeason(month) {
if (month == "Jan" || month == "Feb" || month == "Dec") {
return "winter"
}
else if (month == "Mar" || month == "Apr" || month == "May") {
return "spring"
}
else if (month == "Jun" || month == "Jul" || month == "Aug") {
return "summer"
}
else if (month == "Sep" || month == "Oct" || month == "Nov") {
return "fall"
}
}
function getOutOfSeason(groceryList) {
if (currSeason == "fall") {
for (var i = 0; i < groceryList.length; i++) {
var inSeason = false;
for (var j = 0; j < fall.length; j++) {
if (groceryList[i].name == fall[j]) {
inSeason = true;
}
}
if (!inSeason) {
outOfSeasonList.push(groceryList[i]);
}
}
}
else if (currSeason == "winter") {
for (var i = 0; i < groceryList.length; i++) {
var inSeason = false;
for (var j = 0; j < winter.length; j++) {
if (groceryList[i].name == winter[j]) {
inSeason = true;
}
}
if (!(inSeason)) {
outOfSeasonList.push(groceryList[i]);
}
}
}
else if (currSeason == "summer") {
for (var i = 0; i < groceryList.length; i++) {
var inSeason = false;
for (var j = 0; j < summer.length; j++) {
if (groceryList[i].name == summer[j]) {
inSeason = true;
}
}
if (!inSeason) {
outOfSeasonList.push(groceryList[i]);
}
}
}
else if (currSeason == "spring") {
for (var i = 0; i < groceryList.length; i++) {
var inSeason = false;
for (var j = 0; j < spring.length; j++) {
if (groceryList[i].name == spring[j]) {
inSeason = true;
}
}
if (!inSeason) {
outOfSeasonList.push(groceryList[i]);
}
}
}
return outOfSeasonList;
}
// checks if we have the item in our records
function membershipCheck(item) {
return ((spring.indexOf(item) != -1)
|| (summer.indexOf(item) != -1)
|| (fall.indexOf(item) != -1)
|| (winter.indexOf(item) != -1));
}