Author Topic: Java Script 15 Dan 26 ASYNC , AWAIT, EVENT-LISTENERS  (Read 1942 times)

0 Members and 1 Guest are viewing this topic.

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Java Script 15 Dan 26 ASYNC , AWAIT, EVENT-LISTENERS
« on: 09 November 2025, 21:52:28 »
Spoiler for HTML:
Code: [Select]

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <!-- <script src="main.js" defer></script> -->
     <script src="async-await.js" defer></script>
  </head>

  <body>
    <header>
      <h1>Explore the World 🌎</h1>
    </header>

    <main>
      <div class="search-container">
        <input
          type="text"
          id="country-name"
          placeholder="Enter a country name ..."
        />
        <button id="search-country">Search country</button>
      </div>

      <section class="countries-container">
        <!-- <div class="card">
          <img src="" alt="" />

          <div class="card-info">
            <div class="card-main-info">
              <h3>Name:</h3>
              <h4>Capital:</h4>
            </div>
            <div class="info">
              <p>Region:</p>
              <p>Population:</p>
              <p>Language:</p>
              <p>Currency:</p>
            </div>
            <button class="get">Get</button>
          </div> -->
        </div>
      </section>
     
    </main>

    <footer>
      <p>exploretheworld.com</p>
    </footer>
  </body>
</html>







Spoiler for HTML:


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <!-- <script src="main.js" defer></script> -->
     <script src="async-await.js" defer></script>
  </head>

  <body>
    <header>
      <h1>Explore the World 🌎</h1>
    </header>

    <main>
      <div class="search-container">
        <input
          type="text"
          id="country-name"
          placeholder="Enter a country name ..."
        />
        <button id="search-country">Search country</button>
      </div>

      <section class="countries-container">
        <!-- <div class="card">
          <img src="" alt="" />

          <div class="card-info">
            <div class="card-main-info">
              <h3>Name:</h3>
              <h4>Capital:</h4>
            </div>
            <div class="info">
              <p>Region:</p>
              <p>Population:</p>
              <p>Language:</p>
              <p>Currency:</p>
            </div>
            <button class="get">Get</button>
          </div> -->
        </div>
      </section>
     
    </main>

    <footer>
      <p>exploretheworld.com</p>
    </footer>
  </body>
</html>


discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Re: Java Script 15 Dan 26 ASYNC , AWAIT, EVENT-LISTENERS
« Reply #1 on: 09 November 2025, 21:54:48 »
Spoiler for style CSS:


* {
  box-sizing: border-box;
}

body {
  position: relative;
  margin: 0;
  background: linear-gradient(to top, rgba(255, 255, 255, 0.5)), url("piece.png");
  background-repeat: repeat;
  min-height: 100vh;
  padding-bottom: 5rem;
}
body header {
  background-color: rgb(54, 66, 85);
  padding: 0.8rem;
  display: flex;
  justify-content: center;
  align-items: center;
}
body header h1 {
  color: white;
}
body main {
  padding: 1rem;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
body main .search-container input, body main .search-container button {
  padding: 0.5rem;
  border-radius: 5px;
  font-size: 1.2rem;
  margin-bottom: 1rem;
}
body main .search-container button {
  border: none;
  color: white;
  background-color: rgb(54, 66, 85);
  box-shadow: 2px 0px 10px rgba(0, 0, 0, 0.2);
}
body main .search-container button:hover {
  background-color: rgb(82, 99, 128);
}
body main .search-container button:active {
  transform: translateY(3px);
  box-shadow: 2px 0 20px rgba(0, 0, 0, 0.4);
}
body main .countries-container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  height: 100%;
  gap: 2rem;
  justify-content: center;
  color: white;
}
body main .countries-container .card, body main .countries-container .card-neigbour {
  background-color: rgb(232, 231, 231);
  width: 35%;
  max-width: 350px;
  height: -moz-fit-content;
  height: fit-content;
  max-height: 480px;
  border-radius: 10px;
}
body main .countries-container .card .card-img, body main .countries-container .card-neigbour .card-img {
  width: 100%;
  max-height: 180px;
  border-radius: 10px;
}
body main .countries-container .card .card-info, body main .countries-container .card-neigbour .card-info {
  padding: 5px;
}
body main .countries-container .card .card-info .card-main-info, body main .countries-container .card-neigbour .card-info .card-main-info {
  border-radius: 10px;
  background-color: rgb(190, 210, 243);
  color: rgb(54, 66, 85);
  display: flex;
  flex-direction: column;
  align-items: center;
}
body main .countries-container .card .card-info .card-main-info h2, body main .countries-container .card-neigbour .card-info .card-main-info h2,
body main .countries-container .card .card-info .card-main-info h4,
body main .countries-container .card-neigbour .card-info .card-main-info h4 {
  margin: 0.5rem;
}
body main .countries-container .card .card-info .info, body main .countries-container .card-neigbour .card-info .info {
  color: rgb(54, 66, 85);
}
body main .countries-container .card .get, body main .countries-container .card-neigbour .get {
  background-color: rgb(54, 66, 85);
  color: white;
  padding: 0.5rem;
  border: none;
  border-radius: 5px;
  width: 100px;
}
body main .countries-container .card:hover, body main .countries-container .card-neigbour:hover {
  transform: scale(1.05);
}
body main .countries-container .card-neigbour {
  width: 35%;
  max-width: 300px;
  height: -moz-fit-content;
  height: fit-content;
  max-height: 400px;
}
body footer {
  position: absolute;
  width: 100%;
  bottom: 0;
  background-color: rgb(54, 66, 85);
  display: flex;
  justify-content: center;
  align-items: center;
}
body footer p {
  color: white;
  font-size: 1.2rem;
}/*# sourceMappingURL=style.css.map */






Spoiler for SCSS style:


$default-color: rgb(54, 66, 85);
$text-color: white;
* {
  box-sizing: border-box;
}
body {
  position: relative;
  margin: 0;
  background: linear-gradient(to top, rgba(255, 255, 255, 0.5)),
    url("piece.png");
  background-repeat: repeat;
  min-height: 100vh;
  padding-bottom: 5rem;

  header {
    background-color: $default-color;
    padding: 0.8rem;
    display: flex;
    justify-content: center;
    align-items: center;

    h1 {
      color: $text-color;
    }
  }
  main {
    padding: 1rem;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .search-container {
      input {
        padding: 0.5rem;
        border-radius: 5px;
        font-size: 1.2rem;
        margin-bottom: 1rem;
      }
      button {
        @extend input;
        border: none;
        color: $text-color;
        background-color: $default-color;
        box-shadow: 2px 0px 10px rgba(0, 0, 0, 0.2);

        &:hover {
          background-color: rgb(82, 99, 128);
        }
        &:active {
          transform: translateY(3px);
          box-shadow: 2px 0 20px rgba(0, 0, 0, 0.4);
        }
      }
    }
    .countries-container {
      display: flex;
      flex-wrap: wrap;
      width: 100%;
      height: 100%;
      gap: 2rem;
      justify-content: center;
      color: white;

      .card {
        background-color: rgb(232, 231, 231);
        width: 35%;
        max-width: 350px;
        height: fit-content;
        max-height: 480px;
        border-radius: 10px;

        .card-img {
          width: 100%;
          max-height: 180px;
          border-radius: 10px;
        }
        .card-info {
          padding: 5px;

          .card-main-info {
            border-radius: 10px;
            background-color: rgb(190, 210, 243);
            color: $default-color;
            display: flex;
            flex-direction: column;
            align-items: center;
            h2,
            h4 {
              margin: 0.5rem;
            }
          }
          .info {
            color: $default-color;
          }
        }
        .get {
          background-color: $default-color;
          color: white;
          padding: 0.5rem;
          border: none;
          border-radius: 5px;
          width: 100px;
        }
        &:hover {
          transform: scale(1.05);
        }
      }
      .card-neigbour {
        @extend .card;
        width: 35%;
        max-width: 300px;
        height: fit-content;
        max-height: 400px;
      }
    }
  }

  footer {
    position: absolute;
    width: 100%;
    bottom: 0;
    background-color: $default-color;
    display: flex;
    justify-content: center;
    align-items: center;

    p {
      color: $text-color;
      font-size: 1.2rem;
    }
  }
}


« Last Edit: 09 November 2025, 21:58:09 by efko »
discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Offline efko

  • Administrator
  • Full Member
  • *
  • Topic Author
  • Posts: 1206
  • Country: cs
  • Karma: +18/-1
  • Gender: Male
    • View Profile
    • Awards
  • Time Zone: +1
Re: Java Script 15 Dan 26 ASYNC , AWAIT, EVENT-LISTENERS
« Reply #2 on: 09 November 2025, 21:57:25 »
Spoiler for main JS:


const searchInput = document.getElementById("country-name");
const searchButton = document.getElementById("search-country");
const countries = document.getElementsByClassName("countries-container")[0];
const cardElement = document.getElementsByClassName("card")[0];

function getData(countryName) {
  fetch(`https://restcountries.com/v3.1/name/${countryName}`)
    .then(function (response) {
      console.log(response);
      console.log(typeof response);

      if (!response.ok)
        throw new Error(`Country not found, status:${response.status}`);

      return response.json();
    })
    .then(function (result) {
      console.log(result[0]);
      renderCountry(result[0]);

      const neigbourCounty = result[0].borders ? result[0].borders[0] : null;
      console.log(neigbourCounty);
      if (!neigbourCounty) throw new Error("Neighbour country do not exist! ");

      return fetch(`https://restcountries.com/v3.1/alpha/${neigbourCounty}`);
    })
    .then(function (resolve) {
      return resolve.json();
    })
    .then(function (data) {
      console.log(data[0]);
      renderCountry(data[0], "card-neigbour");
    })
    .catch(function (error) {
      renderError("Something went wrong!", error.message);
    });
}

function renderCountry(item, classCard = "") {
  const language = Object.values(item.languages)[0];
  console.log(language);

  const currency = Object.values(item.currencies)[0];
  currency.template = `${currency.name},\n
   symbol: ${currency.symbol}`;
  console.log(currency.template);

  const cardEl = `
    <div class="card ${classCard}">
          <img class="card-img" src="${item.flags.png}" alt="${
    item.flags.alt
  }" />

          <div class="card-info">
            <div class="card-main-info">
              <h2>${item.name.common}</h2>
              <h4>Capital: ${item.capital[0]}</h4>
            </div>
            <div class="info">
              <p>Region: ${item.region}</p>
              <p>Population: ${(+item.population / 1000000).toFixed(2)} mil</p>
              <p>Language: ${language}</p>
              <p>Currency: ${currency.template}</p>
            </div>
          </div>
        </div>`;

  countries.insertAdjacentHTML("beforeend", cardEl);
}

searchInput.addEventListener("keyup", function (event) {
  if (event.key === "Enter") {
    handlerFunction();
  }
});

searchButton.addEventListener("click", handlerFunction);

function resetInput() {
  searchInput.value = "";
}

function handlerFunction() {
  if (searchInput.value.trim()) {
    getData(searchInput.value);
    resetInput();
  } else {
    alert("Unesite vrednot u input element!");
  }
}

function renderError(message, errMsg) {
  countries.innerHTML += `${message} -> ${errMsg}`;
}

//II primer - renderovanje drzava po regionu
function getDataI(region) {
  fetch(`https://restcountries.com/v3.1/region/${region}`)
    .then((response) => {
      if (!response.ok)
        throw new Error(`Country not found, status:${response.status}`);
      return response.json();
    })
    .then((result) => {
      console.log(result);
      for (const state of result) {
        renderCountry(state);
      }
    })
    .catch((error) => {
      renderError("Something went wrong!", error.message);
    });
}

// getDataI("asia");







Spoiler for async-await JS:


const searchInput = document.getElementById("country-name");
const searchButton = document.getElementById("search-country");
const countries = document.getElementsByClassName("countries-container")[0];
const cardElement = document.getElementsByClassName("card")[0];

function renderCountry(item, classCard = "") {
  const language = Object.values(item.languages)[0];
  const currency = Object.values(item.currencies)[0];
  currency.template = `${currency.name},\n
   symbol: ${currency.symbol}`;
  const cardEl = `
    <div class="card ${classCard}">
          <img class="card-img" src="${item.flags.png}" alt="${
    item.flags.alt
  }" />

          <div class="card-info">
            <div class="card-main-info">
              <h2>${item.name.common}</h2>
              <h4>Capital: ${item.capital[0]}</h4>
            </div>
            <div class="info">
              <p>Region: ${item.region}</p>
              <p>Population: ${(+item.population / 1000000).toFixed(2)} mil</p>
              <p>Language: ${language}</p>
              <p>Currency: ${currency.template}</p>
            </div>
          </div>
        </div>`;

  countries.insertAdjacentHTML("beforeend", cardEl);
}
searchInput.addEventListener("keyup", function (event) {
  if (event.key === "Enter") {
    handlerFunction();
  }
});

searchButton.addEventListener("click", handlerFunction);

function resetInput() {
  searchInput.value = "";
}
function handlerFunction() {
  if (searchInput.value.trim()) {
    fetchData(searchInput.value);
    resetInput();
  } else {
    alert("Unesite vrednot u input element!");
  }
}

function renderError(message, errMsg) {
  countries.innerHTML += `${message} -> ${errMsg}`;
}

async function fetchData(countryName) {
  try {
    const response = await fetch(
      `https://restcountries.com/v3.1/name/${countryName}`
    );
    if (!response.ok)
      throw new Error(`Country not found, status:${response.status}`);
    const result = await response.json();
    renderCountry(result[0]);
    const neigbourCountry = result[0].borders ? result[0].borders[0] : null;

    if (!neigbourCountry) throw new Error("Neighbour country don't exist!");

    const neigbourResponse = await fetch(
      `https://restcountries.com/v3.1/alpha/${neigbourCountry}`
    );
    const dataNeighbour = await neigbourResponse.json();

    renderCountry(dataNeighbour[0]);
  } catch (err) {
    renderError("Something went wrong!", err.message);
  }
}

//try-catch

// let x = 1;
// const y = 2;
// x = 3;
// // y = 4;
// try {
//   let x = 1;
//   const y = 2;
//   x = 3;
//   y = 4;
// } catch (err) {
//   console.log(err);
// }

// k = 5;
// console.log(k);

discord: efko#5979
╚►They say "Before you die your whole life flashes before your eyes…". Make it worth watching. It's true, even for a blind man... ◄╝
              ╚►It is said some lives are linked across time, connected by an ancient calling that echoes through the ages ◄╝

Tags:
 


SimplePortal 2.3.7 © 2008-2026, SimplePortal