We'll be using a file represented as JSON for this assignment. JSON is a standard for formatting data, and it's a common format you'll see throughout your web development career. Even more important will be parsing JSON.
This large object is in the products js file. It is the kind of object you would really recieve from google shopping.
Before you begin writing any code, it's very important to understand the structure of the products
object.
Use the console to look at this object. When you type the product
variable into the console, it becomes interactive.
Paste in the code that can access certain parts of the object and see what values, if any, you get out.
Example cats[1].allergies[6]
- if you don't see anything back up- try the thing above: cats[1].allergies
-
install a sublime package if you want to be able to see the object more clearly: https://packagecontrol.io/packages/SyntaxFold
-
it allows you to "fold" your code to see the format of it.
-
If you type in the variable name
products
in the google chrome developer console, you can see and inspect all the items in the product object. -
When in doubt, think about the different functions available for strings, objects, and arrays.
-
Double-check your results, especially with #3
REMEMBER: data should be passed in to the function as arguments and out as a return value. DO NOT access/modify variables defined outside of the function.
-
Fork and clone this repository
-
write the first basic function
getItems
ininputHappened
-
work on each section individually. when you get one section done, comment it out and work on another section.
-
each function is called inside inputHappened
-
write your function and test it without any user input
var getItemsByBrand = function(items, brand){
// some code
};
// just call the function right here and console.log to test
var result = getItemsByBrand(allItemsArray, "Nikon");
console.log( result );
// rest of code down here:
var inputHappened = function(currentInput){
console.log( currentInput );
.
.
.
- move your function into the
inputHappened
function to make it a complete user program.
Create the following functions. Display the results to the user.
Inside of inputHappened
you will need to deal with the return value of your functions.
Try returning the value of your completed function. What shows on the screen?
You will be required to loop through the result and format a string value that contains the information you want to show. You can use the characters \n
if you want text to go on another line.
- input: json object
- returns: an array of items
Create a function called getItems
. It returns the items
array from the google product object.
Note all other functions (below) use the return of this function (the items array) as their input data.
Your code should call this function by default in inputHappened
.
var inputHappened = function(currentInput){
console.log( currentInput );
// before you begin the code below:
var allItemsArray = getItems( products );
// do stuff with items array
return "WOW GOOGLE SHOPPING";
};
/*
* getItemsByBrand
*
* function purpose
* takes an item array returns a new array of all items of a specified brand.
*
* data description
* items - array of all item objects
* brand - a brand to search for
*
* input example
* var items = [
* {
* // some item object
* }, ... ]
* var brand = "Sony";
*
* function signature
* items (array), brand (string) --> items in brand (array)
*
* functional examples
* var sonyItems = getItemsByBrand( items, "Sony" ); // returns all items that are Sony
*/
Test this function by searching for Sony, Canon, Nikon and Panasonic.
/*
* getItemsByAuthor
*
* function purpose
* takes an item array returns a new array of all items of a specified author.
*
* data description
* items - array of all item objects
* author - a "store" to search for
*
* input example
* var items = [
* {
* // some item object
* }, ... ]
* var author = "Target";
*
* function signature
* items (array), author (string) --> items by author (array)
*
* functional examples
* var targetItems = getItemsByAuthor( items, "Target" ); // returns all items that are Sony
*/
Test this function by searching for Target, CDW, eBay
/*
* getAvailableProducts
*
* function purpose
* takes an item array returns a new array of all items that are available
* an available product is one with at least one availability of "inStock" in the inventories array
*
* data description
* items - array of all item objects
*
* input example
* var items = [
* {
* // some item object
* }, ... ]
*
* function signature
* items (array), --> items by author (array)
*
* functional examples
* var availableItems = getAvailableItems( items ); // returns all available items
*/
If no results were found, let the user know.
Ask the user if they want to do or see the following things, then do that thing based on what the user entered:
-
The number of product items
-
The country of each item
-
Total price of all inventory
-
Search for one of the things above
Let the user enter in a maximum or minimum price. Only show them items that correspond.
Let the user enter in a maximum and minimum price. Only show them items that correspond.
- All content is licensed under a CC-BY-NC-SA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.