Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.4 kB
3
Indexable
let btnMaxim = document.querySelector(".btn__maxim");
let btnVizualizare = document.querySelector(".btn__vizualizare");
let list = document.querySelector(".list");
let selectInput = document.getElementById("#selector");

const data = [
  { tara: "RO", an: 2016, pop: 1041256 },
  { tara: "US", an: 2013, pop: 331915073 },
  { tara: "FR", an: 2016, pop: 6699872484 },
  { tara: "RO", an: 2016, pop: 1041256 },
  { tara: "US", an: 2017, pop: 9931915073 },
  { tara: "FR", an: 2018, pop: 9999879484 },
];

btnVizualizare.addEventListener("click", () => {
  list.innerHTML = "";
  data.forEach(item => {
    if (item.an == selectInput.value) {
      let listItem = document.createElement("li");
      listItem.append(item.tara + " - " + item.pop + " locuitori");
      list.append(listItem);
    }
  });
});

btnMaxim.addEventListener("click", () => {
  console.log("des");
  let popMaxim = maxPopulation(data);
  console.log(popMaxim);
  let p = document.createElement("p");
  let text =
    "Populatia maxima a fost atinasa in anul " +
    popMaxim.an +
    " in tara :" +
    popMaxim.tara;
  p.append(text);
  document.body.appendChild(p);
});


function maxPopulation(date) {
  let max = date[0];
  for (let i = 1; i < date.length; i++) {
    if (max.pop < data[i].pop) {
      max = data[i];
    }
  }
  return max;
}