the problem with your code is that it uses cy-commands together with synchronous statements. Cy-commands get queued and run at a unpredictable moment.
The solution is to pass the value of toCountryCount to a then of the cy-command that and put the calculation (the for statement) in the .then().