Solidity Syntax Error – Expected Primary Expression Fix

remixsolidity

I'm trying to make a function that includes a for loop and an if/if else statement and am running into errors. The syntax regarding where to put the brackets here confuses me.

function decider(nbaTeam[] memory team) public {
    for(uint i=0; i < 31; i++) {
    if (team[i].oRtg > avgOrtg && team[i].dRtg < avgDrtg) {
        niceTeams.push(team);
    }
    }   else if (team[i].oRtg <= avgOrtg && team[i].dRtg >= avgDrtg) {
        naughtyTeams.push(team);
    }
    }

"Expected primary expression." error on the else if statement line.

Best Answer

If you indent the code properly you'll see that there's an extra } before the else if. In solidity braces are used to surround block of codes {}.

function decider(nbaTeam[] memory team) public {
    for (uint i=0; i < 31; i++) {
        if (team[i].oRtg > avgOrtg && team[i].dRtg < avgDrtg) {
            niceTeams.push(team);    
        } else if (team[i].oRtg <= avgOrtg && team[i].dRtg >= avgDrtg) {
            naughtyTeams.push(team);
        }
    }
}
Related Topic