forked from wdi-sg/google-shopping-conditionals-loops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
145 lines (95 loc) · 4.11 KB
/
script.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
137
138
139
140
141
142
143
//Part 1
// var count = 0;
var items = products.items;
// for (var i = 0; i < items.length; i++) {
// if (items[i].kind === "shopping#product") {
// count += 1;
// }
// }
//console.log(count);
// Part 2
// for (var i = 0; i < items.length; i++) {
// var itemInventory = items[i].product.inventories[0].availability;
// if (itemInventory === "backorder" ) {
// console.log(items[i].product.title);
// }
// }
// Part 3 - Print the title of all items with more than one image link.
// for (var i =0; i < items.length; i++) {
// var itemImage = items[i].product.images;
// if (itemImage.length > 1) {
// console.log(items[i].product.title);
// }
// }
// Part 4 - Print all "Canon" products in the items (careful with case sensitivity).
// for (var i =0; i < items.length; i++) {
// var itemBrand = items[i].product.brand;
// if (itemBrand === "Canon") {
// console.log(items[i].product.title);
// }
// }
// Part 5 - Print all items that have an author name of "eBay" and are brand "Canon".
// for (var i = 0; i < items.length; i++) {
// var author = items[i].product.author.name;
// var brand = items[i].product.brand;
// if (author === "eBay" && brand === "Canon") {
// console.log(items[i])
// }
// }
//Part 6 - Print all the products with their brand, price, and an image link
// for (var i = 0; i < items.length; i++) {
// var brand = items[i].product.brand;
// var price = items[i].product.inventories[0].price
// var itemImage = items[i].product.images;
// var randomImglink = Math.floor(Math.random() * itemImage.length);
// console.log(`Brand: ${brand} \n Price: ${price} \n Image: ${itemImage[randomImglink].link}`);
// }
/* Further
Prompt the user for the product brand and print only those products.
Prompt the user if they want to see only new or used items.*/
// var inputBrand = prompt("Enter a brand");
// var inputBrandEdited = inputBrand[0].toUpperCase() + inputBrand.substring(1).toLowerCase();
// var inputCondition = prompt("Do you prefer 'new' or 'used'");
// var inputConditionEdited = inputCondition.toLowerCase();
// for (var i = 0; i < items.length; i++ ) {
// var brand = items[i].product.brand;
// var price = items[i].product.inventories[0].price
// var itemImage = items[i].product.images;
// var randomImglink = Math.floor(Math.random() * itemImage.length);
// var condition = items[i].product.condition;
// if ( brand === inputBrandEdited && condition === inputConditionEdited) {
// console.log(`Brand: ${brand} \n Price: ${price} \n Condition: ${condition} \n Image: ${itemImage[randomImglink].link}`);
// }
// }
/* Further
Prompt the user what kind of search they want to do- search by brand or search by condition.
Then prompt the user to put in ther actual search value- (new/used for condition or brand name for brand)
Print out some special error text (e.g. "Sorry, nothing found") if there were no results.*/
var brandsArray = [];
var conditionArray = [];
var prompt1 = prompt("Would you prefer to search by 'brand' or 'condition");
var promptEdited = prompt1.toLowerCase();
if (promptEdited === "brand") {
var input = prompt("Enter a brand");
var inputEdited = input[0].toUpperCase() + input.substring(1).toLowerCase();
} else if (promptEdited === "condition") {
var input = prompt("Do you prefer 'new' or 'used'");
var inputEdited = input.toLowerCase();
}
for (var i = 0; i < items.length; i++) {
var brand = items[i].product.brand;
brandsArray.push(brand);
var price = items[i].product.inventories[0].price
var itemImage = items[i].product.images;
var randomImglink = Math.floor(Math.random() * itemImage.length);
var condition = items[i].product.condition;
conditionArray.push(condition);
if (brand === inputEdited || condition === inputEdited ) {
console.log(`Brand: ${brand} \n Price: ${price} \n Condition: ${condition} \n Image: ${itemImage[randomImglink].link}`);
}
}
if (!brandsArray.includes(inputEdited) && promptEdited === "brand") {
console.log("Sorry, nothing found");
} else if (!conditionArray.includes(inputEdited) && promptEdited === "condition") {
console.log("Sorry, nothing found");
}