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

hatta #82

Open
wants to merge 1 commit 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
66 changes: 66 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="products.js"></script>
<script type="text/javascript" src="js/google_shopping_functions.js"></script>
<script type="text/javascript" src="script.js"></script>

<p>
## 1.) getItems(objectData)

* **input:** json object
* **returns:** an array of items

Create a function called `getItems` that simply returns the items array from the google product object.

**Note** all other functions (below) use the return of this function as their input.
</p>
<p>
## 2.) getItemsByBrand(items, brand)

* **input:** an array of items, a string of a brand to filter with
* **returns:** an array of items (of a specific 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.

</p>
<p>
## 3.) getItemsByAuthor(items, author)

* **input:** an array of items, a string of an author to filter with
* **returns:** an array of items (of a specific 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
</p>
<p>
## 4.) getAvailableProducts(items)

* **input:** an array of items
* **returns:** an array of items (that are available)

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)

</p>
<p>
## 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.


</p>
</body>
</html>
82 changes: 73 additions & 9 deletions js/google_shopping_functions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,81 @@

/*
* example function called getItemsCount
* input: accepts full item data
* output: returns the length of the items array
*/
function getItemsCount(itemData) {
return itemData.items.length;
// */
// function getItemsCount(itemData) {
// return itemData.items.length;
// }


// * Define and use your functions here


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


// Part 1 getItems(objectData)

function getItem(objectData){
var array = [];
for (var i=0; i<objectData.items.length;i++){
array.push(objectData.items[i]);
}
return array;
}

/*
* Define and use your functions here
*/

// output item count using the getItemsCount function
console.log('Item Count: ' + getItemsCount(data));
// Part 2 getItemsByBrand(items, brand)

function getItemsByBrand(items,brand){
var array = [];
for (var i=0; i<items.length;i++){
if (items[i].product.brand == brand){
array.push(items[i]);
}
}
return array;
}

// Part 3 getItemsByAuthor(items, author)

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

// Part 4 getAvailableProducts(items)

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

function further1() {
console.log(itemArray.length);
}

function further2() {
countryArray = [];
for (var i = 0; i < itemArray.length; i++) {
countryArray.push(itemArray[i].product.country);
} console.log(countryArray);
}

function further3() {
totalPrice = 0;
for (var i = 0; i < itemArray.length; i++) {
totalPrice = totalPrice + itemArray[i].product.inventories[0].price;
} console.log(totalPrice);
}
41 changes: 41 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

let itemArray = getItem(products);
//Q1
console.log(itemArray);


let brandArray = getItemsByBrand(itemArray, "Canon");
//Q2
console.log(brandArray);


let authorArray = getItemsByAuthor(itemArray, "eBay");
//Q3
console.log(authorArray);


let availableArray = getAvailableProducts(itemArray);
//Q4
console.log(availableArray);


let sonyArray = getItemsByBrand(itemArray, "Sony");
//Q5.a
console.log(sonyArray);


let sonyInStockArray = getAvailableProducts(sonyArray);
//Q5.b
console.log(sonyInStockArray);

let adoramaArray = getAvailableProducts(itemArray);
let adoramaInStockArray = getItemsByAuthor(adoramaArray, "Adorama Camera");
//Q5.c
console.log(adoramaInStockArray);


let nikonArray = getItemsByBrand(itemArray, "Nikon");
let nikonEbayArray = getItemsByAuthor(nikonArray, "eBay");
//Q5.d
console.log(nikonEbayArray);