Skip to content

Instantly share code, notes, and snippets.

@ninjayoto
Forked from danomoseley/eBay average price
Created May 1, 2021 23:55
Show Gist options
  • Select an option

  • Save ninjayoto/11ebb6762e30ef080dfc0c8d7ee70c3f to your computer and use it in GitHub Desktop.

Select an option

Save ninjayoto/11ebb6762e30ef080dfc0c8d7ee70c3f to your computer and use it in GitHub Desktop.
Find an average sold price of an eBay search.Do a search for a product, check the "Sold listings" option under "Show only" in the left navigation.Run this script by pasting it into the console tab of the chrome developer tools.
function Stats(arr) {
var self = this;
var theArray = arr || [];
//http://en.wikipedia.org/wiki/Mean#Arithmetic_mean_.28AM.29
self.getArithmeticMean = function() {
var sum = 0, length = theArray.length;
for(var i=0;i<length;i++) {
sum += theArray[i];
}
return sum/length;
}
//http://en.wikipedia.org/wiki/Standard_deviation
self.getStandardDeviation = function() {
var arithmeticMean = this.getArithmeticMean();
var sum = 0, length = theArray.length;
for(var i=0;i<length;i++) {
sum += Math.pow(theArray[i]-arithmeticMean, 2);
}
return Math.pow(sum/length, 0.5);
}
self.setArray = function(arr) {
theArray = arr;
return self;
}
self.getArray = function() {
return theArray;
}
return self;
}
var listings = {};
var soldPrices = [];
$('.bidsold').each(function(k,v)
{
if ($(this).find('.sboffer').length > 0) {
return;
}
var listingId = $(v).closest('li').attr('listingid');
var price = parseFloat($(v).html().trim().replace('$','').replace(',',''));
listings[listingId] = {
'price': price,
'total': price
};
soldPrices.push(price);
});
//console.log(listings);
//console.log(soldPrices);
var stat = Stats(soldPrices);
var mean = stat.getArithmeticMean().toFixed(2);
var stdDev = stat.getStandardDeviation().toFixed(2);
var message = 'Mean: $' + mean + '\n';
message += 'Standard Deviation: $' + stdDev + '\n';
var goodDeal = 0.5;
message += 'Mean minus ' + goodDeal*100 + '% standard deviation (good deal): $' + (mean - (stdDev*goodDeal)).toFixed(2) + '\n';
message += 'Mean minus one standard deviation (great deal): $' + (mean - stdDev).toFixed(2);
alert(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment