Created
June 28, 2019 14:28
-
-
Save jordangarcia/021a7e80f75afecd38d0fe4ae35dd985 to your computer and use it in GitHub Desktop.
This file contains 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
// prettier-ignore | |
const PRODUCTS_URL = "https://horizons-school-of-technology.github.io/week02/day4/examples/products.json"; | |
function getAllProductDetails(productUrls, callback) { | |
let products = []; | |
for (let i = 0; i < productUrls.length; i++) { | |
let url = productUrls[i].url; | |
$.ajax(url, { | |
success: function(productDetail) { | |
products.push(productDetail); | |
if (products.length === productUrls.length) { | |
callback(products); | |
} | |
} | |
}); | |
} | |
} | |
function formatPriceInCents(number) { | |
return `$${String(number / 100)}`; | |
} | |
function insertProducts(products) { | |
products.sort(function(a, b) { | |
return a.priceCents - b.priceCents; | |
}); | |
const ul = $("<ul>"); | |
products.forEach(product => { | |
ul.append( | |
$(`<li>${product.name}, ${formatPriceInCents(product.priceCents)}</li>`) | |
); | |
}); | |
$("#products") | |
.empty() | |
.append(ul); | |
} | |
$.ajax(PRODUCTS_URL, { | |
success: function(productUrls) { | |
getAllProductDetails(productUrls, function(products) { | |
insertProducts(products); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment