1

first, I apologize for this very simple question. I'm starting my studies on smart contract can someone explain highest voted function in this contract?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
pragma experimental ABIEncoderV2;

contract Voting{

    string[] public members;
    mapping(string => uint) public memberVote;
    mapping(address => bool) public voted;

    function addMember(string memory name) public {
        bool repeated=false;
        for(uint i=0; i < members.length; i++){
            if(keccak256(abi.encodePacked((name))) == keccak256(abi.encodePacked(members[i]))){
                repeated = true;
            }
        }
        if(repeated==false){
            members.push(name);
            memberVote[name] = 0;
        }
    }

    function Vote(string memory name) public{
        require(voted[msg.sender]==false, 'one address one vote');
        memberVote[name] = memberVote[name]+1;
        voted[msg.sender]=true;
    }

    function highestVoted() public view returns(string memory){
        uint maxIndex=0;
        uint maxVote=0;
        for(uint i = 0; i<members.length; i++){
            if(memberVote[members[i]]>maxVote){
                maxIndex=i;
                maxVote=memberVote[members[i]];
            }
        }
        return members[maxIndex];
    }

    function getMembers() public view returns(string[] memory){
        return members;
    }
    function getVotes(string memory member) public view returns(uint){
        return memberVote[member];
    }

}

2 Answers 2

1

No need to apologize :) we all start somewhere!

The highestVoted function in this contract is used to find the member with the highest number of votes. Here's a breakdown of how it works:

Initialize two variables, maxIndex and maxVote, to store the highest-voted member's index and vote count.

   uint maxIndex=0; // Index of the highest-voted member
   uint maxVote=0; // Count of votes

Loop through the members array with a for loop.

for(uint i = 0; i<members.length; i++)

For each member, check if their vote count is greater than the current maxVote.

if(memberVote[members[i]]>maxVote)

If a member's vote count is higher, update maxIndex to their index and maxVote to their vote count.

maxIndex=i; // Update the maxIndex
maxVote=memberVote[members[i]]; // Update maxVote (higher than the previous)

After the loop completes, return the member with the highest vote count using members[maxIndex].

  return members[maxIndex];

This function determines the current leader in the voting process.

0

Highest Voted Function Contract function that counts and performs a specific action when a certain number of votes is achieved in the system.

addMember(string memory name) public Vote(string memory name) public

function addMember(string memory name) public { // function body here }

function Vote(string memory name) public { // function body here }

highestVoted() public view returns(string memory) function highestVoted() public view returns (string memory) { uint maxIndex = 0; uint maxVotes = 0;

for (uint i = 0; i < members.length; i++) {
    if (memberVote[members[i]] > maxVotes) {
        maxVotes = memberVote[members[i]];
        maxIndex = i;
    }
}

return members[maxIndex];

getMembers() public view returns(string[] memory) function getMembers() public view returns (string[] memory) { string[] memory activeMembers = new string; uint activeIndex = 0; for (uint i = 0; i < members.length; i++) { if (memberVote[members[i]] > 0) { activeMembers[activeIndex] = members[i]; activeIndex++; } } return activeMembers; }

getVotes(string memory member) public view returns(uint) function getVotes(string memory member) public view returns (uint) { require(memberVote[member] > 0, "Member does not exist or has no votes."); return memberVote[member]; }

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 2, 2023 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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