From 4c105dba0714564c2a31dac5ded8e994d0625dd1 Mon Sep 17 00:00:00 2001 From: varunagarwal315 Date: Thu, 3 Nov 2016 13:02:25 +0530 Subject: Update solidity-by-example.rst Might be trivial, but makes more sense to be able to directly return the name of the winner for the election. If the position of the winner on the proposal[] array is returned, then people still don't know the name of the person who won. --- docs/solidity-by-example.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'docs/solidity-by-example.rst') diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst index 2e53b78c..dd208ea3 100644 --- a/docs/solidity-by-example.rst +++ b/docs/solidity-by-example.rst @@ -170,6 +170,22 @@ of votes. } } } + + function winnerName() constant + returns (bytes32 winnerName) + { + //Init a for loop that compares all the votes + //one at a time. If a higher count is found, the + //value is updated. p represents position of the + //proposed person's name in the array + uint winningVoteCount = 0; + for (uint p = 0; p < proposals.length; p++) { + if (proposals[p].voteCount > winningVoteCount) { + winningVoteCount = proposals[p].voteCount; + winnerName = proposals[p].name; + } + } + } } Possible Improvements -- cgit