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 From 38e0b0a5faf75e3392fbfdd371b27db728026853 Mon Sep 17 00:00:00 2001 From: varunagarwal315 Date: Thu, 3 Nov 2016 17:42:05 +0530 Subject: Update solidity-by-example.rst made changes suggested to simplify code. Hope this is enough Thanks --- docs/solidity-by-example.rst | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'docs/solidity-by-example.rst') diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst index dd208ea3..f5f109fd 100644 --- a/docs/solidity-by-example.rst +++ b/docs/solidity-by-example.rst @@ -171,20 +171,13 @@ of votes. } } + //Calls winningProposal() function to get the index + //of the winner contained in the proposals array and then + //returns the name of the winner 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; - } - } + winnerName = proposals[winningProposal()].name; } } -- cgit From 364698255aef1638e8d3e917dfc5a577312e6d07 Mon Sep 17 00:00:00 2001 From: varunagarwal315 Date: Thu, 3 Nov 2016 22:25:19 +0530 Subject: Update solidity-by-example.rst added the space. Sorry, just slipped my mind. --- docs/solidity-by-example.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'docs/solidity-by-example.rst') diff --git a/docs/solidity-by-example.rst b/docs/solidity-by-example.rst index f5f109fd..915cfa76 100644 --- a/docs/solidity-by-example.rst +++ b/docs/solidity-by-example.rst @@ -171,9 +171,9 @@ of votes. } } - //Calls winningProposal() function to get the index - //of the winner contained in the proposals array and then - //returns the name of the winner + // Calls winningProposal() function to get the index + // of the winner contained in the proposals array and then + // returns the name of the winner function winnerName() constant returns (bytes32 winnerName) { -- cgit