Created
August 13, 2019 01:55
-
-
Save harrisonmalone/b1987736b80b2fdd402c363c799baea9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require('axios'); | |
| // make the array that we'll push our responses into | |
| let arr = [] | |
| // hitting the api function | |
| const getData = async (nextEndpoint) => { | |
| // sometimes we don't get the next endpoint | |
| let endpoint; | |
| if (nextEndpoint === undefined) { | |
| endpoint = '/api/products/1' | |
| } else { | |
| endpoint = nextEndpoint | |
| } | |
| const url = `http://wp8m3he1wt.s3-website-ap-southeast-2.amazonaws.com${endpoint}` | |
| return await axios.get(url) | |
| } | |
| const accessData = async (nextEndpoint) => { | |
| if (arr.length === 0) { | |
| // nextEndpoint doesn't exist here | |
| const result = await getData() | |
| arr.push(result.data) | |
| // accessData will now take a nextEndpoint as an argument | |
| accessData(result.data.next) | |
| } else { | |
| // here next endpoint does exist | |
| const result = await getData(nextEndpoint) | |
| arr.push(result.data) | |
| if (result.data.next === null) { | |
| // if it is null then we run our filtering functions | |
| const products = getOnlyProducts(arr) | |
| // ensuring that we only have product objects in our array, no next strings | |
| const airConditioners = filterAirconditioners(products) | |
| // now we have just air conditioners, we've filtered out everything else | |
| /* | |
| write a function that will do the math, return average cubic weight | |
| your code here | |
| */ | |
| } else { | |
| // we call access data again with the nextEndpoint as an argument | |
| accessData(result.data.next) | |
| } | |
| } | |
| } | |
| const getOnlyProducts = (arr) => { | |
| const products = [] | |
| arr.forEach((data) => { | |
| data.objects.forEach((product) => { | |
| products.push(product) | |
| }) | |
| }) | |
| return products | |
| } | |
| const filterAirconditioners = (products) => { | |
| return products.filter((product) => { | |
| console.log(product.category) | |
| return product.category === "Air Conditioners" | |
| }) | |
| } | |
| // start program | |
| accessData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment