2020-09-30

Sep 30 In-Class Exercise Thread.

Post your solutions to the Sep 30 In-Class Exercise to this thread.
Best,
Chris
Post your solutions to the Sep 30 In-Class Exercise to this thread. Best, Chris

-- Sep 30 In-Class Exercise Thread
var mapFunction1 = function() { total_quantity = 0; for(let i in this.items){ total_quantity += this.items[i].qty; } emit(this.cust_id, [this.price, total_quantity]); }; var reduceFunction1 = function(keyCustId, price_quantity_arr) { return (Array.sum(price_quantity_arr[0])/Array.sum(price_quantity_arr[1])); };
(Edited: 2020-09-30)
<nowiki> var mapFunction1 = function() { total_quantity = 0; for(let i in this.items){ total_quantity += this.items[i].qty; } emit(this.cust_id, [this.price, total_quantity]); }; var reduceFunction1 = function(keyCustId, price_quantity_arr) { return (Array.sum(price_quantity_arr[0])/Array.sum(price_quantity_arr[1])); }; </nowiki>

-- Sep 30 In-Class Exercise Thread
	var mapFunction2 = function() {
		emit(this.cust_id, this.items);
	};
	var reduceFunction2 = function(keyCustId, valuesItems) {
		item_price_sum = 0;
		item_count = 0;
		for item in valuesItems {
			item_price_sum += valuesItems[item].price * valuesItems[item].qty;
			item_count += valuesItems[item].qty;
		}
		return (float)item_price_sum/item_count;
	};
(Edited: 2020-09-30)
var mapFunction2 = function() { emit(this.cust_id, this.items); }; var reduceFunction2 = function(keyCustId, valuesItems) { item_price_sum = 0; item_count = 0; for item in valuesItems { item_price_sum += valuesItems[item].price * valuesItems[item].qty; item_count += valuesItems[item].qty; } return (float)item_price_sum/item_count; };

-- Sep 30 In-Class Exercise Thread
 mapFunction1 = () => {
   itemCount = 0;
   itemPrice = 0;
   for (let i: this.items){
      let {price} = i;
      itemCount += 1;
      itemPrice += price;
   }
    let average = itemPrice / itemCount;
   emit(this.cust_id, average);
 };
 reduceFunction1 = (keyCustId, average) => {
   return average;
};(Edited: 2020-09-30)
mapFunction1 = () => { itemCount = 0; itemPrice = 0; for (let i: this.items){ let {price} = i; itemCount += 1; itemPrice += price; } let average = itemPrice / itemCount; emit(this.cust_id, average); }; reduceFunction1 = (keyCustId, average) => { return average; };

-- Sep 30 In-Class Exercise Thread
 var mapFunciton1 = function() {
 	var items = this.items; // array of items
 	for(item in items) {
 		emit(this.cust_id, {qty: item.qty, price: item.price});
 	}
 };
 var reduceFunction1 = function(keyCustId, item) {
 	var totalItems += item.qty;
	var totalPrice += item.price
	return totalPrice/totalItems;
 };
var mapFunciton1 = function() { var items = this.items; // array of items for(item in items) { emit(this.cust_id, {qty: item.qty, price: item.price}); } }; var reduceFunction1 = function(keyCustId, item) { var totalItems += item.qty; var totalPrice += item.price return totalPrice/totalItems; };

-- Sep 30 In-Class Exercise Thread
 var mapFunction = function() {
  
 	let qty = 0;
 	for(let k in this.item){
 		qty+=this.item[k].qty;
 	}
 	
 	emit(this.cust_id, this.price, qty);
 };
 var reduceFunction = function(cust_id, price, qty) {
 	return price/qty;
 };
var mapFunction = function() { let qty = 0; for(let k in this.item){ qty+=this.item[k].qty; } emit(this.cust_id, this.price, qty); }; var reduceFunction = function(cust_id, price, qty) { return price/qty; };

-- Sep 30 In-Class Exercise Thread
 function mapFunction(){ emit(this.cust_id, this.items); };
 
 function reduceFunction(keyCustId, items){
    var itemPrice = 0;
    var quantity = 0; items.forEach(function (item) 
     {
      itemPrice += item.price* item.quantity;
      quantity += item.quantity;
     });
    return itemPrice/quantity;
 };
(Edited: 2020-09-30)
function mapFunction(){ emit(this.cust_id, this.items); }; function reduceFunction(keyCustId, items){ var itemPrice = 0; var quantity = 0; items.forEach(function (item) { itemPrice += item.price* item.quantity; quantity += item.quantity; }); return itemPrice/quantity; };

-- Sep 30 In-Class Exercise Thread
 var mapFunction1 = function() {
    this.items.forEach(function(item){ 
                           emit(this.cust_id, {price: item.price, qty: item.qty}); 
                      });
 };
 var reduceFunction1 = function(keyCustId, itemQtyAndPrices) {
    return Array.sum(itemQtyAndPrices.price)/Array.sum(itemQtyAndPrices.qty);
 };
var mapFunction1 = function() { this.items.forEach(function(item){ emit(this.cust_id, {price: item.price, qty: item.qty}); }); }; var reduceFunction1 = function(keyCustId, itemQtyAndPrices) { return Array.sum(itemQtyAndPrices.price)/Array.sum(itemQtyAndPrices.qty); };
2020-10-01

-- Sep 30 In-Class Exercise Thread
 var mapFunction = function() {
 	emit(this.cust_id, this.item);
 };
 var reduceFunction = function(keyCustId, item) {
 	var totalItems += item.qty;
	var totalPrice += item.price
	return totalPrice/totalItems;
 };
var mapFunction = function() { emit(this.cust_id, this.item); }; var reduceFunction = function(keyCustId, item) { var totalItems += item.qty; var totalPrice += item.price return totalPrice/totalItems; };
2020-10-04

-- Sep 30 In-Class Exercise Thread
 var mapFunction = function() {
     emit(this.cust_id, this.items);
 };
  
 var reduceFunction = function(keyCustId, items) {
     sum = 0;
     count = 0;
     for item in items {
	 sum += items[item].price * items[item].qty;
	 count += items[item].qty;
     }
     return sum/count;
 };
var mapFunction = function() { emit(this.cust_id, this.items); }; var reduceFunction = function(keyCustId, items) { sum = 0; count = 0; for item in items { sum += items[item].price * items[item].qty; count += items[item].qty; } return sum/count; };
[ Next ]
X