Skip to content

Instantly share code, notes, and snippets.

@alexey-sh
Created October 27, 2016 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexey-sh/33d235b4b2e5f2d7f0216e7e373adcfc to your computer and use it in GitHub Desktop.
Save alexey-sh/33d235b4b2e5f2d7f0216e7e373adcfc to your computer and use it in GitHub Desktop.
Find median price on drom ru
function median (values) {
values.sort(function (a, b) {
return a - b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2)
return values[half];
else
return (values[half - 1] + values[half]) / 2.0;
}
var data = [];
$('.newCatList.newCatList_v2').first().find('tr[data-row-as-link]').each(function () {
var me = $(this);
var cn = me.find('.c_n');
var engine = cn.next().clone();
var power = engine.find('span').remove().text();
var details = engine.text().replace(/\s+/g, ' ').trim();
var o = details.split(' ')[0];
data.push({
v: o,
details: details,
power: power,
price: me.find('.b-tableCell__link .f14').text(),
name: cn.find('.b-tableCell__link').text().replace(/\s+/g, ' ').trim()
});
});
console.log(data);
var prices = data.map(function (a) {
return parseInt(a.price.replace(/\s+/g, '').trim(), 10);
});
console.log(prices, median(prices));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment