I have made a struct which has an address called playersAddress
and a uint called numbers
.
I then have a fixed sized array which has in it the playersAddress
and the numbers.
When someone calls the function pickWinner
, the highest number in the numbers
variable compared with the other numbers will transfer a certain amount of ether to the address in the playersAddress.
Also the numbers in the numbers
variable are always changing value. I would like to know in this situation would the only way to find the highest number in an array would be to use a loop? I have tried to write out a for loop in the pickWinner
function, but it keeps coming up with an error.
function getPickWinner() public {
var test = numbers;
var largest = 0;
var i;
for (i = 0; i < test.length; i++) {
if (test[i] > largest) {
largest = test[i];
}
}
The error comes up in the var i;
part and it says assignment necessary for type detection.
I would like to know what this means and how I could fix it? At the moment I am not worrying about gas or anything I just want to experiment a bit first. If gas is a really big issue would it be possible in this scenario to do the loop offchain but keep everything else the way it is ? How could I possibly go about doing it offchain? thank you