E
E
Eugene4542020-08-21 15:26:51
JavaScript
Eugene454, 2020-08-21 15:26:51

Should my code be rewritten with classes? If so, how?

I made the crypto tracker very primitive. Works with API https://coinmarketcap.com/api/ . And I thought that I could rewrite it with Classes, but I don’t quite understand how to do it.

//Standart crypto
const cryptoCurrencyArray = [
  "Bitcoin",
  "Ethereum",
  "Chainlink",
  "VeChain",
  "Binance Coin",
];

const cards = document.querySelector(".crypto-app__cards");

function getInfoAboutCrypto() {
  fetch(
    `https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=${apiKey}`
  )
    .then((res) => res.json())
    .then((data) => {
      data.data.forEach((el) => {
        for (let i = 0; i < cryptoCurrencyArray.length; i++) {
          if (cryptoCurrencyArray[i] === el.name) {
            const cardPrice = Math.round(el.quote.USD.price * 100) / 100;
            const percentChange =
              Math.round(el.quote.USD["percent_change_7d"] * 100) / 100;
            createCard(el.name, cardPrice, percentChange);
            break;
          }
        }
      });
    });
}

function createCard(cryptoName, price, percentChange) {
  let cardClass;
  if (parseFloat(percentChange) < 0) {
    cardClass = "cardRed";
  } else {
    cardClass = "cardGreen";
  }
  cards.innerHTML += `
  <div class="card ${cardClass}">
  <div class="card-header">
    ${cryptoName}
  </div>
  <div class="card-price">
    ${price}$
  </div>
  <div class="card-percent-change">
    ${percentChange}%
  </div>
</div>
  `;
}

getInfoAboutCrypto();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question