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

Herda's Google Shopping Functions #85

Open
wants to merge 3 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>
</head>

<body>
<script src="products.js"></script>
<script src="js/google_shopping_functions.js"></script>
<script src="js/script.js"></script>

</body>
</html>
109 changes: 99 additions & 10 deletions js/google_shopping_functions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,106 @@

/*
* example function called getItemsCount
* input: accepts full item data
* output: returns the length of the items array
*/
// ONE
// Create a function called getItems that simply returns the items array from the google product object.
function getItems(itemData) { // objectData is products
return itemData.items;
}

// Create a function called getItemsByBrand that takes an item array returns a new array of all items of a specified brand.
function getItemsByBrand(itemsArray, brand) { // brand will be user input
var brandArray = [];
for (var i = 0; i < itemsArray.length; i++ ) {
//var title = itemsArray[i]["product"]["title"]; // if you want to print the title aka name of products
var availBrand = itemsArray[i]["product"]["brand"];

if (availBrand === brand) {
//brandArray.push(title);
brandArray.push(itemsArray[i])
}
}
return brandArray;
}


// Create a function called getItemsByAuthor that takes an item array and returns a new array of all items by a specified author.
function getItemsByAuthor(itemsArray, author) {
var authorArray = [];
for (var i = 0; i < itemsArray.length; i++ ) {
var authorName = itemsArray[i]["product"]["author"]["name"];

if (authorName === author) {
authorArray.push(itemsArray[i]);
}
}
return authorArray;
}


// 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(itemsArray) {
var availProds = [];
for (var i = 0; i < itemsArray.length; i++ ) {
var inventories = itemsArray[i]["product"]["inventories"];

for (j = 0; j < inventories.length; j++) {
var availability = inventories[j]["availability"];

if (availability === "inStock") {
availProds.push(itemsArray[i]["product"]);
}
}
}
return availProds
}

/////////////////// FURTHER ////////////////////////


// The number of product items
function getItemsCount(itemData) {
return itemData.items.length;
}

/*
* Define and use your functions here
*/
// The country of each item
function getItemCountry(itemsArray) {
var itemCountry = {};
for (var i = 0; i < itemsArray.length; i++ ) {
country = itemsArray[i]["product"]["country"];
title = itemsArray[i]["product"]["title"];

itemCountry[title] = country;
}
return itemCountry;
}

// Total price of all inventory
function totalInventoryPrice(itemsArray) {
var total = 0;
for (var i = 0; i < itemsArray.length; i++ ) {
var inventories = itemsArray[i]["product"]["inventories"];

// output item count using the getItemsCount function
console.log('Item Count: ' + getItemsCount(data));
for (j = 0; j < inventories.length; j++) {
var price = inventories[j]["price"];

total += price;
}
}
return total;
}

// price range using max/min
function getItemsAccToRange(max, min) {
var itemsInRange = [];
for (var i = 0; i < itemsArray.length; i++ ) {
var inventories = itemsArray[i]["product"]["inventories"];

for (j = 0; j < inventories.length; j++) {
var price = inventories[j]["price"];

if (min < price && max > price) {
itemsInRange.push(itemsArray[i]);
}
}
}
return itemsInRange;
}

82 changes: 82 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var itemsArray = getItems(products);
////////////////////////// CALLING OF FUNCTIONS //////////////////////
// Call getItemsByBrand that takes an item array returns a new array of all items of a specified brand.
//console.log(getItemsByBrand(itemsArray, "Canon"));

// Call getItemsByAuthor that takes an item array and returns a new array of all items by a specified author.
//console.log(getItemsByAuthor(itemsArray, "Target"));
//getItemsByAuthor(itemsArray, "Target");

// Call 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(itemsArray));

//////////////////// END OF CALLING OF FUNCTIONS //////////////////////////////

/////////////////////// USE YOUR FUNCTIONS ////////////////////////////////
// All items made by Sony.
var itemsBySony = getItemsByBrand(itemsArray, "Sony");
//console.log(itemsBySony);


// All items made by Sony that are available.
var availSony = getAvailableProducts(itemsBySony);
//console.log(availSony);


// All available items by the author "Adorama Camera"
var adorama = getItemsByAuthor(itemsArray, "Adorama Camera");
var availAdorama = getAvailableProducts(adorama)
if (availAdorama.length === 0) {
//console.log("No stock.")
} else {
//console.log(availAdorama)
}


// All items made by Nikon with the author eBay.
var itemsByNikon = getItemsByBrand(itemsArray, "Nikon");
var nikonByEbay = getItemsByAuthor(itemsByNikon, "eBay");
if (nikonByEbay.length === 0) {
//console.log("Invalid item.")
} else {
//console.log(nikonByEbay)
}

//////////////////////////// END OF USE YOUR FUNCTIONS EXERCISE //////////////////////////////


/////////////////////////////// FURTHER EXERCISE /////////////////////////
// Prompt the user for the search term they are looking for.
var choice = Number(prompt("Enter 1 for Number of Products \nEnter 2 for Country of each Item \nEnter 3 for Total Price of Inventory \nEnter 4 to Search for Items" ))
if (choice === 1) {
console.log("Items count: " + itemsArray.length);
} else if (choice === 2) {
console.log(getItemCountry(itemsArray));

} else if (choice === 3) {
console.log("Total price: " + totalInventoryPrice(itemsArray));

} else if (choice === 4) {
searchChoice = Number(prompt("Enter 1 to Search by Brand \nEnter 2 to Search by Author \nEnter 3 to Search with Max and Min price range"))

if (searchChoice === 1) {
var brand = prompt("Enter brand")
var capBrand = brand.charAt(0).toUpperCase() + brand.slice(1) // to auto capitalise the word
console.log(getItemsByBrand(itemsArray, capBrand));

} else if (searchChoice === 2) {
var author = prompt("Enter author name");
console.log(getItemsByAuthor(itemsArray, author));
}

else if (searchChoice === 3) {
var max = Number(prompt("Enter max price"));
var min = Number(prompt("Enter min price")) ;
console.log(getItemsAccToRange(max, min));
}
}

/////////////////// END OF FURTHER /////////////////////////////////