Skip to content

Instantly share code, notes, and snippets.

@fiksn
Created August 25, 2018 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiksn/bdafc8e6197bf8abfcf3403f9ae49524 to your computer and use it in GitHub Desktop.
Save fiksn/bdafc8e6197bf8abfcf3403f9ae49524 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.24;
contract CookieMonster {
struct Cookies {
uint num;
bool exists;
}
mapping (address => Cookies) map;
address[] addresses;
function addCookies(uint number) public {
if (!map[msg.sender].exists) {
addresses.push(msg.sender);
map[msg.sender].exists = true;
}
map[msg.sender].num += number;
}
function numCookies() public view returns (uint) {
uint sum = 0;
for(uint i=0; i<addresses.length; ++i) {
sum += map[addresses[i]].num;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment