How do I pass the coin object returned from the displayCurrencies function to the getCoinId function as a parameter for making an API call to retrieve specific coin data?
This is the function created to return the value:
let returnID = (value) => {
return value;
};
This is the function where I want to return the coin from:
let displayCurrencies = async () => {
let coinsContainer = document.querySelector(`.coins`);
try {
let coins = await getData();
let coinsArray = [];
let coinElement;
for (const coin of coins) {
coinElement = coin;
if (coinsArray.length > 20) {
break;
}
coinsArray.push(coin);
// create Nodes
let coinDisplay = createElement(`li`, `coin`);
let coinSymbolElement = createElement(`p`, `coinSymbol`);
let coinIDElement = createElement(`p`, `coinID`);
// set Values
coinSymbolElement.innerHTML = coin.symbol;
coinIDElement.innerHTML = coin.id;
// append
coinDisplay.append(coinSymbolElement, coinIDElement);
coinsContainer.appendChild(coinDisplay);
coinDisplay.addEventListener(`click`, () => {
openModal();
returnID(coin);
});
}
let returnCoin = returnID
coinDisplay.addEventListener(`click`, () => {
console.log(returnCoin);
});
console.log(returnCoin);
} catch (error) {
console.log(error);
}
};
And finally, this is the function in which I want to use the returned value:
displayCurrencies();
let getCoinId = async () => {
let coinID = await displayCurrencies();
let currencyData = `https://api.coingecko.com/api/v3/coins/${coinID}`;
let responseData = await fetch(currencyData);
let dataOfCoins = await responseData.json();
console.log(dataOfCoins);
};