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


Use utilities to solve such kinda problems. No builtin functions available.

Considering Off-chain calculations, yes it would be great if we calculate/transfer assets among users in our system. And assign only their true assets by submitting on main chain finally when they leave our system.

PS: Like Plasma MVP


var is deprecated. Give your variables types. In particular, use uint256 i to silence this, but go ahead and give all the variables types:

uint256 test = numbers;
uint256 largest = 0;
uint256 i;

Note: If you are using var for a declaration of the variable i then you need to assign value to variable at the time of declaration like var i=0. I suggest you to use uint instead of var for i and largest variable.


This code will do what you ask.

Note the variable declarations. var is deprecated.

pragma solidity ^0.4.7;

contract test3 {

    uint256[5] public numbers = [1, 2, 100, 4, 5];

    function getPickWinner() public view returns(uint256){
        uint256 largest = 0; 
        uint256 i;

        for(i = 0; i < numbers.length; i++){
            if(numbers[i] > largest) {
                largest = numbers[i]; 
        return largest;

I have set the variable numbers just for testing purposes. Hope this help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.