Last active
August 29, 2015 14:24
-
-
Save pofigizm/6f8c3adae7baeb5f2572 to your computer and use it in GitHub Desktop.
Задание №2
This file contains 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
/** | |
* Реализация API, не изменяйте ее | |
* @param {string} url | |
* @param {function} callback | |
*/ | |
function getData(url, callback) { | |
var RESPONSES = { | |
'/countries': [ | |
{name: 'Cameroon', continent: 'Africa'}, | |
{name :'Fiji Islands', continent: 'Oceania'}, | |
{name: 'Guatemala', continent: 'North America'}, | |
{name: 'Japan', continent: 'Asia'}, | |
{name: 'Yugoslavia', continent: 'Europe'}, | |
{name: 'Tanzania', continent: 'Africa'} | |
], | |
'/cities': [ | |
{name: 'Bamenda', country: 'Cameroon'}, | |
{name: 'Suva', country: 'Fiji Islands'}, | |
{name: 'Quetzaltenango', country: 'Guatemala'}, | |
{name: 'Osaka', country: 'Japan'}, | |
{name: 'Subotica', country: 'Yugoslavia'}, | |
{name: 'Zanzibar', country: 'Tanzania'}, | |
], | |
'/populations': [ | |
{count: 138000, name: 'Bamenda'}, | |
{count: 77366, name: 'Suva'}, | |
{count: 90801, name: 'Quetzaltenango'}, | |
{count: 2595674, name: 'Osaka'}, | |
{count: 100386, name: 'Subotica'}, | |
{count: 157634, name: 'Zanzibar'} | |
] | |
}; | |
setTimeout(function () { | |
var result = RESPONSES[url]; | |
if (!result) { | |
return callback('Unknown url'); | |
} | |
callback(null, result); | |
}, Math.round(Math.random * 1000)); | |
} | |
/** | |
* Ваши изменения ниже | |
*/ | |
var requests = ['/countries', '/cities', '/populations']; | |
var responses = {}; | |
var input = window.prompt('Input something (city, country or continent):'); // may be 'Africa' | |
function _exist(object){ | |
return function(key){ | |
return object.hasOwnProperty(key); | |
}; | |
} | |
function _compare(value, key){ | |
return function(element){ | |
return value === (key ? element[key] : element); | |
}; | |
} | |
function _compareSome(array, key){ | |
return function(element){ | |
return array.some(_compare(key ? element[key] : element)); | |
}; | |
} | |
function _add(key){ | |
return function(result, element){ | |
return result += element[key]; | |
}; | |
} | |
function _get(key){ | |
return function(element){ | |
return element[key]; | |
}; | |
} | |
function getPopulations(data, input){ | |
var populations, | |
countries, | |
cities; | |
// search input in cities | |
populations = data['/populations'] | |
.filter(_compare(input, 'name')); | |
if (populations.length) { | |
return populations; | |
} | |
// search input in countries | |
cities = data['/cities'] | |
.filter(_compare(input, 'country')) | |
.map(_get('name')); | |
populations = data['/populations'] | |
.filter(_compareSome(cities, 'name')); | |
if (populations.length) { | |
return populations; | |
} | |
// search input in continents | |
countries = data['/countries'] | |
.filter(_compare(input, 'continent')) | |
.map(_get('name')); | |
cities = data['/cities'] | |
.filter(_compareSome(countries, 'country')) | |
.map(_get('name')); | |
populations = data['/populations'] | |
.filter(_compareSome(cities, 'name')); | |
if (populations.length) { | |
return populations; | |
} | |
return []; | |
} | |
requests.forEach(function(request){ | |
getData(request, function(error, result){ | |
responses[request] = result; | |
// check all responses | |
if ( requests.every(_exist(responses)) ) { | |
var population = getPopulations(responses, input).reduce(_add('count'), 0); | |
console.log('Total population in', input, ':', population); | |
} | |
}); | |
}) | |
// Проблема кода была в том что цикл for не создает скоп для каждого прохода | |
// и в момент выполнения callback переменная request будет всегда ссылаться | |
// на requests[3] | |
// Простой способ починить - обернуть тело цикла в самовызывающуюся функцию | |
// ugly code | |
/* | |
for (i = 0; i < 3; i++) { | |
(function(){ | |
var request = requests[i]; | |
var callback = function (error, result) { | |
responses[request] = result; | |
var l = []; | |
for (K in responses) | |
l.push(K); | |
if (l.length == 3) { | |
var c = [], cc = [], p = 0; | |
for (i = 0; i < responses['/countries'].length; i++) { | |
if (responses['/countries'][i].continent === 'Africa') { | |
c.push(responses['/countries'][i].name); | |
} | |
} | |
for (i = 0; i < responses['/cities'].length; i++) { | |
for (j = 0; j < c.length; j++) { | |
if (responses['/cities'][i].country === c[j]) { | |
cc.push(responses['/cities'][i].name); | |
} | |
} | |
} | |
for (i = 0; i < responses['/populations'].length; i++) { | |
for (j = 0; j < cc.length; j++) { | |
if (responses['/populations'][i].name === cc[j]) { | |
p += responses['/populations'][i].count; | |
} | |
} | |
} | |
console.log('Total population in African cities: ' + p); | |
} | |
}; | |
getData(request, callback); | |
})(); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment