Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Shopping Functions #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Google Shopping Functions</title>
<script src="products.js"></script>
<script src="js/google_shopping_functions.js"></script>
<script src="script.js"></script>
</head>
<body>

</body>
</html>
96 changes: 94 additions & 2 deletions js/google_shopping_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,105 @@
* output: returns the length of the items array
*/
function getItemsCount(itemData) {
return itemData.items.length;
return itemData.length;
//console.log(itemData);
}

/*
* Define and use your functions here
*/

// output item count using the getItemsCount function
console.log('Item Count: ' + getItemsCount(data));
console.log('Item Count: ' + getItemsCount(products.items));

//1.) getItems(objectData)
//Create a function called `getItems` that simply returns the items array from the google product object.

function getItems(objectData) {
return products.items;
}

//2.) getItemsByBrand(items, brand)
//Create a function called `getItemsByBrand` that takes an item array returns a new array of all items of a specified brand.
//Test this function by searching for Sony, Canon, Nikon and Panasonic.

// var results = [];
// var itemByProduct = products["items"];

function getItemsByBrand (items, brand) {
var results = [];

for (i=0; i < items.length; i++){
// console.log(items[i])
// if (items[i]["brand"] === "Sony" || itemByProduct[i]["brand"] === "Canon" || itemByProduct[i]["brand"] === "Nikon" || itemByProduct[i]["brand"] === "Panasonic") {
// results.push(itemByProduct);
// }
if (items[i].product.brand === brand) {
results.push(items[i].product.title);
}
}
return results;
}
//console.log(results);

//console.log(getItemsByBrand(products.items, "Canon"));

//3.) getItemsByAuthor(items, author)
// Create a function called `getItemsByAuthor` that takes an item array and returns a new array of all items by a specified author.
// Test this function by searching for Target, CDW, eBay

function getItemsByAuthor (items, author) {
var results = [];
for (j=0; j < items.length; j++) {
if (items[j].product.author.name === author) {
results.push(items[j].product.title);
}
}
return results;
}

//4.) getAvailableProducts(items)
//Create function called `getAvailableProducts` that takes an item array and returns an array containing all of the available products (an available product is one with at least one availability of "inStock" in the inventories array).

function getAvailableProducts (items) {
var results = [];
for (k=0; k < items.length; k++) {
if (items[k].product.inventories[0].availability === "inStock") {
results.push(items[k].product.title)
}
}
return results;
}

// 5.

function getAvailableSonyProducts (items) {
var results = [];
for (l=0; l < items.length; l++) {
if (items[l].product.inventories[0].availability === "inStock" && items[l].product.brand === "Sony") {
results.push(items[l].product.title)
}
}
return results;
}

// function getEbayNikonProducts (items) {
// var results = [];
// for (m=0; m < items.length; m++) {
// if (items[m].product.author.name === "eBay" && items[m].product.brand === "Nikon") {
// results.push(items[m].product.title)
// }
// }
// return results;
// }











41 changes: 41 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// console.log(products);

// // 1. Returns the items array from the google product object.

// console.log(getItems(products));

// // 2. Create a function called `getItemsByBrand` that takes an item array returns a new array of all items of a specified brand.

// console.log(getItemsByBrand(products.items, "Sony"));
// console.log(getItemsByBrand(products.items, "Canon"));
// console.log(getItemsByBrand(products.items, "Nikon"));
// console.log(getItemsByBrand(products.items, "Panasonic"));

// // 3. Create a function called `getItemsByAuthor` that takes an item array and returns a new array of all items by a specified author.
// //Test this function by searching for Target, CDW, eBay.

// console.log(getItemsByAuthor(products.items, "Target"));
// console.log(getItemsByAuthor(products.items, "CDW"));
// console.log(getItemsByAuthor(products.items, "eBay"));

// // 4. Create function called `getAvailableProducts` that takes an item array and returns an array containing all of the available products (an available product is one with at least one availability of "inStock" in the inventories array).

// console.log(getAvailableProducts(products.items));

/*
5.) Use your functions

Use the functions you created in 1 - 4 to ouput (console.log) the following lists of items.

* All items made by Sony.
* All items made by Sony that are available.
* All available items by the author "Adorama Camera"
* All items made by Nikon with the author eBay.

* remember that you must create a script tag for each file you create, and that they must be in the correct order for your code to run.
*/

console.log(getItemsByBrand(products.items, "Sony"));
console.log(getAvailableSonyProducts(products.items));
console.log(getItemsByAuthor(products.items, "Adorama Camera"));
console.log(getItemsByBrand(getItemsByAuthor(products.items, "eBay")), "Nikon");