Created
October 27, 2016 14:13
-
-
Save alexey-sh/33d235b4b2e5f2d7f0216e7e373adcfc to your computer and use it in GitHub Desktop.
Find median price on drom ru
This file contains hidden or 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
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