mercredi 24 janvier 2018

Else statement still running when if statement is true

I have search functionality that allows you to search for cryptocurrencies and I'm trying to have it where if the param/coin that is searched doesn't match with the API's title of the coin or symbol, an alert box would pop up telling the user to try again.

The problem I'm having is that if the if statement is true and there's a match, the else statement/alert box pops up regardless. Here's my code within my axios request:

cryPrice(param){
    axios.get(`https://api.coinmarketcap.com/v1/ticker/?limit=0`)
        .then(res => {
            const cryptos = res.data;
            const price = []

            for(let title in cryptos){
                const cryptoTitle = cryptos[title].name.toUpperCase();
                const cryptoSymbol = cryptos[title].symbol.toUpperCase();

                if(cryptoTitle === param || cryptoSymbol === param){
                    price.push(cryptos[title].price_usd);       
                }else{
                    alert("Please try again");
                    break;
                }
            }
            this.setState({
                priceResults: price
            })
        })
    }

Aucun commentaire:

Enregistrer un commentaire