fork(1) download
  1. var shanhai = { name: "Шанхай", population: 23416 };
  2. var jakarta = { name: "Джакарта", population: 30539 };
  3. var pekin = { name: "Пекин", population: 21009 };
  4. var deli = { name: "Дели", population: 24998 };
  5. var manila = { name: "Манила", population: 24123 };
  6.  
  7. var cities = [shanhai, jakarta, pekin, deli, manila];
  8.  
  9. function sort(city1, city2) {
  10. return city2.population - city1.population; //Заменим местами 1 и 2 и получим уже по возрастанию.
  11. }
  12.  
  13. function populationSort(arr, x) {
  14. var newArr = arr.sort(sort);
  15. newArr = newArr.slice(0, x);
  16. var newNewArr = []; //newNewArr :)
  17.  
  18. for (var i = 0; i < x; i++) {
  19. newNewArr[i] = newArr[i].name;
  20. };
  21. return newNewArr;
  22. }
  23.  
  24. console.log(populationSort(cities, 5));
Runtime error #stdin #stdout #stderr 0.46s 321856KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
js: uncaught JavaScript runtime exception: ReferenceError: "console" is not defined.