Skip to content

Instantly share code, notes, and snippets.

@davericher
Created November 28, 2015 00:18
Show Gist options
  • Save davericher/c71291abc1a3cf403663 to your computer and use it in GitHub Desktop.
Save davericher/c71291abc1a3cf403663 to your computer and use it in GitHub Desktop.
// David Richer - Shopify challenge Winternship 2015
'use strict';
var request = require("request"); // run npm install request
request({
// Shopify JSON feed
url: 'http://shopicruit.myshopify.com/products.json',
json: true // the response will be json
}, function (err, response, items) {
// Halt if response is not valid
if (err || response.statusCode !== 200) {
console.log('Something went wrong with your request');
process.exit(1);
}
// Hold the products
var products = items.products,
item, // Item placeholder
variant, // Variant placeholder
totalAmount = 0.0, // hold the total price
totalItems = 0;
// Iterate through the items
for (item in products) {
// check to see if its a wallet / lamp
if (products.hasOwnProperty(item)) {
if (products[item].product_type.match(/wallet|lamp/ig)) {
// Iterate over variants (colors etc)
for (variant in products[item].variants) {
if (products[item].variants.hasOwnProperty(variant)) {
// Add the price to the total
totalItems += 1; // Increment the amount of items
totalAmount += parseFloat(products[item].variants[variant].price,
10);
}
}
}
}
}
console.log('All', totalItems, 'variants of wallets and lamps would cost', '$' +
totalAmount);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment